Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect that the Shift key has been pressed?

Tags:

macos

cocoa

I have a NSView subclass and I would like it to react when the user presses the ⇧ Shift key. However, -[NSView keyDown:] (which I currently override) isn't called when modifier keys alone are pressed.

How can I be notified when the Shift key has been pressed?

like image 503
zneak Avatar asked Feb 13 '12 21:02

zneak


People also ask

How do I know if shiftKey is pressed?

The mouseEvent shiftKey property is used to define whether the shift key is pressed or not. It is a boolean value. When the shift key is pressed then on click of the mouse left button, it returns true and if the shift key is not pressed then it returns false.

What is shiftKey in Javascript?

Definition and Usage. The shiftKey property returns a Boolean value that indicates whether or not the "SHIFT" key was pressed when a key event was triggered. Note: This property is read-only.


1 Answers

From the Cocoa event handling guide:

The flagsChanged: method can be useful for detecting the pressing of modifier keys without any other key being pressed simultaneously. For example, if the user presses the Option key by itself, your responder object can detect this in its implementation of flagsChanged:.

More details can be found here.

The documentation for NSResponder also states the following:

flagsChanged:

Informs the receiver that the user has pressed or released a modifier key (Shift, Control, and so on).

-- (void)flagsChanged:(NSEvent *)theEvent

like image 155
Benjamin Gale Avatar answered Oct 05 '22 01:10

Benjamin Gale