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

前端 未结 7 1085
甜味超标
甜味超标 2020-12-10 00:37

I have a NSView subclass and I would like it to react when the user presses the ⇧ Shift key. However, -[NSView keyDown:] (which I curren

7条回答
  •  再見小時候
    2020-12-10 01:36

    Checking for pressed:

    -(void)keyDown:(NSEvent *)theEvent
    {
        if ([theEvent modifierFlags] & NSShiftKeyMask)
        {
            NSLog("Shift key was pressed");
        }
    }
    

    Checking for release:

    -(void)keyUp:(NSEvent *)theEvent
    {
        if(!([theEvent modifierFlags] & NSShiftKeyMask))
        {
            NSLog("Shift key was released");
        }
    }
    

    It should be noted, the NSLog function will only be called if SHIFT and then some other key is pressed.

    Hope this helps!

提交回复
热议问题