Objective-C: Listen to keyboard shortcuts and act on them - objective-c

I am developing an app for Mac OS X and I need to listen to keyboard shortcut inputs so I can act on them.
For example:
Up and down keys move up and down on a table view.
⌘ + ⌦ drops an item.
⌘ + ⇧ + N creates a new item.
It shouldn't be restricted to events on the focused control.
Any ideas?
Thanks in advance.

I think your best option* would be +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]. This creates an object which will call a block handler whenever your application receives an event of the specified type. The handling takes place right before your NSApplication dispatches the event to a window, and you have the opportunity to modify the event or stop it from proceeding further.
You can thus catch key down events as they get passed to your app and do whatever you like with them before any controls get a chance to see them. I posted this originally in another question, but here's a snippet for doing things with arrow key presses:
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
switch ([theEvent keyCode]) {
case 123: // Left arrow
NSLog(#"Left behind.");
break;
case 124: // Right arrow
NSLog(#"Right as always!");
break;
case 125: // Down arrow
NSLog(#"Downward is Heavenward");
break;
case 126: // Up arrow
NSLog(#"Up, up, and away!");
break;
default:
break;
}
// Return the event, a new event, or, to stop
// the event from being dispatched, nil
return theEvent;
};
// Creates an object we do not own, but must keep track of so that
// it can be "removed" when we're done; therefore, put it in an ivar.
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
See the Event-Handling Guide for some details about what you're supposed to do with that monitor object. Specifically, Apple apparently "discourages" removing it inside of dealloc, but doesn't give a reason.
*So long as you can require Snow Leopard, at least.

You may need to implements callbacks in your application. Take a look to CGEventTapCreate to start listening for hotkeys.
CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
kCGEventKeyDown
myEventTapCallback,
NULL);
myEventTapCallback should be conform to CGEventTapCallBack and gets called when a key is pressed. Then, inside myEventTapCallback you'll have enough information on keys pressed and you can implement your custom functionality.

Have a look at the Cocoa Event-Handling Guide. There are a few places you can intercept events before they get to the key view. You can intercept all events in the application by overriding -[NSApplication sendEvent:], or you can intercept events at a per-window level by overriding-[NSWindow sendEvent:].

Related

How to prevent window from loosing focus when receiving a grabbed key from X11

My window calls hide() when a QEvent::FocusOut is received. Simultaniously I want its visibility to be toggled if a hotkey is pressed. Now I have the following problem: Pressing the hotkey, registered with XGrabKex(...) seems to steel the focus of my app. Resulting in an unwanted behaviour. If my app is visible the hotkeyevent steels focus, which results in a QEvent::FocusOut, which hides my app, and after that the hotkey is received which toggles visibility (shows) my app. I.e. my app does not hide when pressing the hotkey.
Is there a way to tell the x window system to not steel the focus when a grabbed key is pressed? Or are there other possible solutions to this problem?
A couple of different methods.
Use XQueryKeymap to see which keys are pressed. For instance, when you get a FocusOut event, call XQueryKeymap and see if your hotkey is pressed. If it is not, hide the window; if it is, don't hide it and wait for the hotkey event.
Delay hiding on FocusOut by 100 or so milliseconds. Cancel hiding if you either get the hot key or get your focus back during this time interval.
Look also here for useful info.
Finally got it to work in a "proper" way:
bool MainWidget::nativeEvent(const QByteArray &eventType, void *message, long *)
{
#ifdef Q_OS_LINUX
if (eventType == "xcb_generic_event_t")
{
xcb_generic_event_t* event = static_cast<xcb_generic_event_t *>(message);
switch (event->response_type & 127)
{
case XCB_FOCUS_OUT: {
xcb_focus_out_event_t *fe = (xcb_focus_out_event_t *)event;
if ((fe->mode==XCB_NOTIFY_MODE_GRAB && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR)
|| (fe->mode==XCB_NOTIFY_MODE_NORMAL && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR ))
hide();
break;
}
}
}
#endif
return false;
}

mouse clicks and keystrokes count clicked any where

I want to implement a functionality in my mac application which will return me the mouse clicks and keystrokes count clicked any where (in my application or outside). Please guide me.
Thank You
NSEvent's + (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(void (^)(NSEvent*))block provides this functionality.
Here is a quick example:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask|NSLeftMouseDownMask handler:^(NSEvent *anEvent){
if(anEvent.type==NSKeyDown)
{
NSLog(#"Had key down event: %#",anEvent);
}
if(anEvent.type==NSLeftMouseDown)
{
NSLog(#"Had left mouse down event: %#",anEvent);
}
}];
You could add this somewhere during your apps initialization, for example in you application delegate's -applicationDidFinishLaunching: method.
The above does NOT register events in your application, only in other applications. If you also need events in your app, you can add a local monitor (slightly different as it returns an event):
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask|NSLeftMouseDownMask handler:^NSEvent *(NSEvent *anEvent){
if(anEvent.type==NSKeyDown)
{
NSLog(#"Had local key down event: %#",anEvent);
}
if(anEvent.type==NSLeftMouseDown)
{
NSLog(#"Had local left mouse down event: %#",anEvent);
}
return anEvent;
}];
Also note that according to the NSEvent documentation: Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

Check if user finished sliding on a continuous UISlider?

In my app, I have a couple of UISlider instances to change various values. The values are displayed right next to the slider, as well as rendered in a 3d space in another visible part of the app.
The 3d part includes some rather heavy calculations, and right now it doesn't seem possible to update it live as the slider changes. That would imply that I'd have to set the slider's continuous property to NO, therefore only getting updates when the slider has finished changing.
I'd prefer to have the displayed value update live, however. Is there a way to have a slider that is continuous (so I can update my value-label in real time) and still sends some kind of message once the user has finished interacting with it? My gut feeling right now is to subclass UISlider and override the touchesEnded: method. Is that feasible?
You can do this with simple target/actions.
Set a target and action for the UIControlEventValueChanged event, and then another target and action for the UIControlEventTouchUpInside event. With the continuous property set to YES, the value changed event will fire as the slider changes value, while the touch up inside event will only fire when the user releases the control.
I just had to do this, so I looked up touch properties, and used the full IBAction header.
This should be a viable alternative for people who want some extra control, though Jas's is definitely easier on the code side.
- (IBAction)itemSlider:(UISlider *)itemSlider withEvent:(UIEvent*)e;
{
UITouch * touch = [e.allTouches anyObject];
if( touch.phase != UITouchPhaseMoved && touch.phase != UITouchPhaseBegan)
{
//The user hasn't ended using the slider yet.
}
}
:D
Also note you should connect the UIControlEventTouchUpOutside event as well in case the user drags his finger out of the control before lifting it.
In Swift 3:
#IBAction func sliderValueChanged(_ slider: UISlider, _ event: UIEvent) {
guard let touch = event.allTouches?.first, touch.phase != .ended else {
// ended
return
}
// not ended yet
}

Send auto-repeated key using CoreGraphics methods (Mac OS X Snow Leopard)

I have been successful sending keystrokes in order to automate a particular software package for drawing that I use. This software relies a lot of keyboard shortcuts so I wrote something that could call some of these keyboard shortcuts in order to streamline my workflow. As I said, this has worked out good.
My library is a Cocoa library that is loaded as a plugin to the software package. Here is a snippet of code that I have been using for sending my keystrokes.
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
CGEventRef eventUp = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, false);
//setting the process number somewhere else
CGEventPostToPSN(&number, eventDown);
CGEventPostToPSN(&number, eventUp);
For some procedures in the drawing package if you continue to hold the Shift key then you activate a special tool. I have been unable to simulate this. I thought I could send the Shift key and say that I wanted it to auto-repeat but that doesn't seem to work. I have been using the following code to set the auto-repeat:
//This is done before sending the key
CGEventSetIntegerValueField(eventDown, kCGKeyboardEventAutorepeat, 1);
In my testing I have been unable to make any key auto-repeat. It just send the key once and that is it.
Is there anyone that have been successful autorepeating a key using the above method? I have searched the Internet for answers but all I have found are some unanswered questions from 2008... Any help is greatly appreciated.
Thanks,
mobbe
The code that OP finally came up with to solve the problem (transferred here from a comment under other answer):
CGEventRef flagsChanged = CGEventCreate(eventSource);
CGEventSetType(flagsChanged, kCGEventFlagsChanged);
CGEventSetIntegerValueField(flagsChanged, kCGKeyboardEventKeycode, 56);
CGEventSetFlags(flagsChanged, 131330);
CGEventPostToPSN(&number, flagsChanged);
CFRelease(flagsChanged); CFRelease(eventSource);
131330 is a constant indicating the Shift key; it is related to NSShiftKeyMask and kCGEventFlagMaskShift, which are 131072 (0x00020000). 131330 - 256 - 2 == 131072.
UPDATE: the Shift key's code isn't 56, according to Events.h:
...
kVK_Shift = 0x38,
...
(EDIT: of course those of you who are paying attention (I wasn't) realize that HEX 38 == DEC 56.)
I also realized how to get modifier key presses: flagsChanged:. So using this keycode, I can catch Shift key presses in flagsChanged:, and the repeat works fine; I also get repeated key events for "normal" keys in keyDown: and keyUp: without difficulty.
It sounds like you may not have access to/want to change the event-handling code (to add flagsChanged:) though, so if that keycode doesn't work for you, I'm stumped and can only say "Good luck!"
I believe that the field you're setting is used to indicate not that the event should be repeated by the system, but that an event is an auto-repeat of a previous event. You still have to generate each event yourself. Something like (EDITED to use a timer instead of a loop):
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
// Post the initial keydown
CGEventPostToPSN(&pidNumber, eventDown);
// Keep track of how many Shift keydown events have been sent
shiftKeyRepeatCount = 1;
[NSTimer scheduledTimerWithTimeInterval:0.3 // I don't know exactly what the interval should be, of course
target:self
selector:#selector(repeatShiftKeyDown:)
userInfo:nil
repeats:YES];
CFRelease(eventDown);
- (void)repeatShiftKeyDown:(NSTimer *)tim {
if( shiftKeyRepeatCount >= kRepeatCountForSpecialTool ){
[tim invalidate];
[self sendShiftKeyUp];
return;
}
shiftKeyRepeatCount++;
GEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
// Set the auto-repeat field
CGEventSetIntegerValueField(eventDown, kCGKeyboardEventAutorepeat, 1);
CGEventPostToPSN(&pidNumber, eventDown);
CFRelease(eventDown);
}
I'm not certain whether you can reuse the first event with a changed field or you'll need to generate a new event to use when repeating.

Issue with CGEventTapCreate() call

I'm trying to register for global key events using this code :
void function()
{
CFMachPortRef keyUpEventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventKeyUp),&keyUpCallback,NULL);
CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL, keyUpEventTap, 0);
CFRelease(keyUpEventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(), keyUpRunLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(keyUpRunLoopSourceRef);
}
The application crashes while executing CFMachPortCreateRunLoopSource() call. I think the crash is because of CGEventMaskBit(kCGEventKeyUp) when I create an event tap.
But if I create event tap using CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventFlagsChanged),&keyUpCallback,NULL), the application works fine. It does not crash. I'm getting callbacks when any modifier key is pressed. But I need to get callbacks for delete key pressed.
Any ideas?
Thanks,
Dheeraj.
I think you need special permission to register for keyboard events. I forget off hand what that is, but to test it run the program as root and see if it still crashes.
Edit:
According to this article you must either run the program as root or enable assistive devices.
The crash may just be because CGEventTapCreate returns NULL.