How can I detect that the Shift key has been press

2020-02-10 01:05发布

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?

标签: macos cocoa
6条回答
淡お忘
2楼-- · 2020-02-10 01:32

What do you do if the key press does not come as an event? For example, you want to test the state of the Shift key when the program starts up? The Shift key was pressed before the program started, and will not be released until after you test its state.

In Windows, you can easily get this with a call to GetKeyState(VK_SHIFT). What is the macOS equivalent?

查看更多
时光不老,我们不散
3楼-- · 2020-02-10 01:33

in the apple's documentation :

You have to override the keydown method

with the following NSEvent modifier flags

查看更多
Ridiculous、
4楼-- · 2020-02-10 01:38

Here is the code deals with key event with swift 3.0.1 tested on Xcode 8.2.1 and macOS 10.12.2

override func keyDown(with event: NSEvent) {
    var handled = false
    if event.keyCode == 53 { // ESC, same as `CMD + .`
        handled = true
        print("ESC")
    }
    if event.modifierFlags.contains(.command) { // .shift, .option, .control ...
        if let chars = event.charactersIgnoringModifiers {
            handled = true // likely we are interested with that key
            switch chars {
            case "r":
                print("CMD + r")
            case ",":
                print("CMD + ,")
            case "/":
                print("CMD + /")
            default:
                handled = false
            }
        }
    }
    if !handled {
        super.keyDown(with: event) // let system handle it(may contains sound)
    }
}
查看更多
Animai°情兽
5楼-- · 2020-02-10 01:38

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!

查看更多
我只想做你的唯一
6楼-- · 2020-02-10 01:40

Here is an example, modified slightly from Matt Gemmell's ModKeyTest sample app. Create a basic Cocoa app with one button and hook up the button to an IBAction like this. Then try out your desired combination of keys. The docs are a bit fuzzy, but Matt's example is very clear and presents all you need to leverage this further from the docs.

- (IBAction)myAction:(id)sender {
NSUInteger flags = [[NSApp currentEvent] modifierFlags];
if ((flags & NSCommandKeyMask) && (flags & NSAlternateKeyMask) && (flags & NSControlKeyMask)) {
    NSBeginInformationalAlertSheet(@"Modifier keys Command Option Control detected", nil, nil, nil, [NSApp mainWindow], self, nil, nil, nil,
                                   @"You sneaky thing!");
}

if ((flags & NSCommandKeyMask) && (flags & NSShiftKeyMask)) {
    NSBeginInformationalAlertSheet(@"Modifier keys Command Shift detected", nil, nil, nil, [NSApp mainWindow], self, nil, nil, nil,
                                   @"You sneaky thing!");
}

if ((flags & NSAlphaShiftKeyMask)) {
    NSBeginInformationalAlertSheet(@"Modifier keys Caps Lock detected", nil, nil, nil, [NSApp mainWindow], self, nil, nil, nil,
                                   @"You sneaky thing!");
}
if ((flags & NSFunctionKeyMask)) {
    NSBeginInformationalAlertSheet(@"Modifier keys fn detected", nil, nil, nil, [NSApp mainWindow], self, nil, nil, nil,
                                   @"You sneaky thing!");
}
查看更多
够拽才男人
7楼-- · 2020-02-10 01:42

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

查看更多
登录 后发表回答