In JavaScript for Automation (JXA), we can detect if the ctrl key is pressed with

$.NSEvent.modifierFlags & $.NSControlKeyMask

If ctrl is not pressed, the return will be 0. If it is, the value will be different.

But NSControlKeyMask is deprecated. I found NSEventModifierFlagControl but can’t get it to work.

What is the current way to detect if the ctrl key is pressed?

  • Just because it is deprecated doesn't mean it won't work. Did this code stop working? If not there is no urgent need to update it. Functions can be deprecated for years before they are removed. When you say it doesn't work what happens? Does it compile? if so, what is the value that the '&' operator is returning? Does it always return 0? – Ɱark Ƭ Feb 23 at 16:11
  • @ⱮarkƬ It means it always returns the same value, but I’m not even sure I’m using it correctly, hence the vagueness. The urgency of wanting to update the code doesn’t factor into it. It has already been deprecated for over two years, and I’ll only know if it stopped working after I upgrade my system (which is over two years behind). Postponing updating deprecated code is an accident waiting to happen. I’d rather have it correct now than have it break from under me when I really need it. – user137369 Feb 23 at 17:43
  • @user137369 Please post the full code on how you register for and get the event. It is hard to diagnose the problem without seeing your actual code. – jksoegaard Feb 23 at 19:25
  • @jksoegaard That is the full code. Those two lines are all there is to it. – user137369 Feb 24 at 1:22

Per your latest comment, you state that the code you have quoted works in practice, and your only problem is you want to get rid of the deprecated NSControlKeyMask.

Simply replace that word with NSEventModifierFlagControl and this is it.

The only thing Apple did is to change the name. In recent macOS NSControlKeyMask is even defined as simply being equal to NSEventModifierFlagControl. Both have the value 1 << 18 (i.e. the 18th bit is set to 1, rest is 0).

  • There’s nothing wrong with the presented code. That code is, as stated, working. It doesn’t do anything with the condition for simplicity of the question, but it does return different codes, which proves it works. What I’m asking for is the method to replace the deprecated one. – user137369 Feb 24 at 22:57
  • You're contradicting yourself now. You replied to MarkT saying that it always returns the same value - now you're saying it returns different values. Without the correct information, it is hard to help you? – jksoegaard Feb 24 at 23:03
  • I have changed the answer so that it now has the correct answer according to your latest statement that your old code is in fact working, returning different values. – jksoegaard Feb 24 at 23:11
  • “You replied to MarkT saying that it always returns the same value”. That was in reply to “When you say it doesn't work what happens?”, which was referring to NSEventModifierFlagControl. – user137369 Feb 24 at 23:21
  • Okay, that doesn't really make any sense. Why ask if a constant would change? So you have basically misunderstood eachother. Anyways, I have given you the correct answer to the revised question now, so you can remove the downvote. – jksoegaard Feb 24 at 23:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .