how to get Apple “command button” key code programmatically?

时间秒杀一切 提交于 2020-01-15 23:06:21

问题


i need to know what key code of Command button in mac keyboard, do somebody know how to get it programmatically?

can i get the key code if user tap button Command + X (cut shortcut)? thank you for the suggestion


回答1:


I'm going to assume here that you're dealing with NSEvents generated by the AppKit framework.

In NSEvent's documentation, take a look at the modifierFlags method.

And the flag you're lookng for is specifically the NSCommandKeyMask.

Now, to get it, if you have a NSView view in focus... it inherits from NSResponder. One of the methods in NSResponder is keyDown. So add a keyDown method to your subclassed view and the parameter it takes is a NSEvent. And that is how you would get your command button key.

BTW & FYI, if you just want to get the command key by itself (which is what I suspect), that's a bigger trick and I'm not sure if it's possible with AppKit. Because the command key, like the control and shift keys, are modifier keys and that means when events get generated, NSEvent is expecting two keys to be pressed at the same time (e.g. Command + Q for Quit, Control + C for interrupt, Shift + A for capital A).



来源:https://stackoverflow.com/questions/10648957/how-to-get-apple-command-button-key-code-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!