how to get Apple "command button" key code programmatically? - macos

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

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).

Related

GTK4 under rust, trying to find which mouse pointer and whether there are modifer keys pressed

I'm writing a gtk4 application in rust, and I'd like to get the mouse key that was clicked and any modifier keys at the time. My GestureClick handler has .buttons(0) to accept input from all buttons, but the event object does not seem to have any way to determine the source of the event. The event just gives the GestureClick object, location, and click count. Does it need to have a separate handler for each button?
In addition, I'd like to use modifier keys like shift and ctrl to modify the action. This does not seem to be available (at least, I haven't been able to find it). Would I need to monitor keypress events to keep track of the state myself? I'm pretty sure X reports the state of the modifier keys on click events, gtk doesn't?
Thanks

How to send keypress combinations in nightwatch

I can send a single key press in night-watch without a problem, but I need to press combination of them. For example UP_ARROW + SHIFT
Code from page objects.
this.sendKeys('#pmField', this.api.Keys.UP_ARROW+this.api.Keys.SHIFT)
This function just sends keys in a sequence. Firstly arrow up and then shift and I'm expecting that they would be pressed together as a combination.
browser
.keys(browser.Keys.CONTROL) // hold the control
.click('#element') // click something
.keys(browser.Keys.NULL) // release the control
This works properly in my tests when i need to click multiple elements while holding the control key. I think you can combine it with following key strokes instead of clicks. Hope this helps.
As Skuubi80 states you can use browser.keys().
Take a look at the api doc and note this line...
Rather than the setValue, the modifiers are not released at the end of the call. The state of the modifier keys is kept between calls, so mouse interactions can be performed while modifier keys are depressed.
Keep in mind this does mean that you will need to call browser.keys('null') to 'un-press' the keys once done.
Hope that helps :)

Continuous key press event in Silverlight application

I am making application using silverlight.Here i want to move line on screen continuously on left and right arrow key pressed. In silverlight application there is facility of RepeatButton but i am not getting how to use it. Please help me if there is any solution on that.Thanks in advance.
An easier method would be to have (bool) flags like "keyLeftPressed" and "keyRightPressed". Then, on a specific tick (from a DispatchTimer possibly), check if the flags are set and take appropriate action. Finally, use four events to set the condition of the two flags: left key pressed, left key released, right key pressed, right key released.

Can not detect ESC key in Cocoa Application?

I use onKeyDown in a Custom NSView in detect keydown events, it works well when I input the normal keys, like "a, b, c", but it does not invoke onkeydown function when I press ESC, I want to exit my application when user press ESC.
How to do that?
OK, it turns out my keyboard is broken, only for ESC key, I changed with a keyboard, it works now. Thank you anyway.
I just dropped in a KeyDown handler into a custom NSView in one of my test apps and the ESC key is hitting KeyDown perfectly fine.
Seems like something to do with the way you're calling KeyDown, I think? Maybe the focus isn't set correctly.
In any event, another thing you can do is to implement cancelOperation: in your custom NSView.
Here is the documentation for [NSResponder cancelOperation:]. This also responds to the Escape key and to the Macintosh standard Command + . key combination.
Also check out the Handling Key Events section of the Cocoa Event-Handling Guide, which is where I was looking for answers for you. Hope this helps!

cmd + period(.) does not work as expected

I develop an application on Mac using cocoa. I need to handle cmd + period(.) keyboard event as a command I designed. But now the cmd + period(.) keyboard event does not well handled as I expected.
In the cocoa keyEvent handle process, if the application object processes a key event and it turns out not to be a key equivalent or a key interface control event, it then sends it to the key window in a sendEvent: message. The window object invokes the keyDown: method in the first responder. My handle for cmd + period(.) is in keyDown: method.
But the problem is that Mac treates cmd + period(.) key the same as Escape key. The key window first searches the view hierarchy for a view whose key equivalent is Escape or Command-., whichever was entered. But none of these views handles the key equivalent, then a cancel: action message is sent to the first responder in the responder chain.
So cmd + period(.) is handled as a cancel operation before it reaches keyDown: method.
Can anyone have some idea to solve this problem. And make the cmd + period(.) be handled as I expected but as cancel command. Thank you.
What is more, it is better not handle the cmd + period(.) when do the key equivalent check(performkeyEquivalent).
If you want to override the default handling, you need to catch the keyboard event earlier in the chain. For example, subclass NSWindow and override -sendEvent, or even more thorough, subclass NSApplication and override -nextEventMatchingMask (all events will pass through this function).

Resources