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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With