(Mac) Simulating keyboard shortcuts - Arrow keys don't work - objective-c

I'm trying to use Quartz Event Services to programmatically trigger keyboard shortcuts:
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef leftDown = CGEventCreateKeyboardEvent(src, 0x7b, true); // creating left arrow down event
CGEventRef leftUp = CGEventCreateKeyboardEvent(src, 0x7b, false); // creating left arrow up event
CGEventSetFlags(leftDown, kCGEventFlagMaskCommand); // setting command key modifier flag
CGEventSetFlags(leftUp, kCGEventFlagMaskCommand); // ""
CGEventTapLocation loc = kCGHIDEventTap;
CGEventPost(loc, leftDown);
CGEventPost(loc, leftUp);
CFRelease(leftDown);
CFRelease(leftUp);
CFRelease(src);
This code works for triggering system functions, if you use something other than an arrow key. But if you do use an arrow key, the code will just make your cursor move instead.
Triggering, for example, command-space with this code (0x31, kCGEventFlagMaskCommand) successfully brings up spotlight, but if you map spotlight to command-left_arrow, and then trigger command-left_arrow with this code, it will just make the cursor jump to the start of the line.
Something interesting to note is that Apple Script behaves the exact same way when I try to trigger system shortcuts incorporating arrow keys with it.
Is there a workaround for this?
Thank you for your help, I appreciate it.

For some reasons the arrow keys only work with a "private" CGEventSourceStateID in the way that you expect it. Change the line
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
to
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStatePrivate);
and it will work as expected. (Tested on macOS 10.13.4)

Related

Handle VueJS event only when key is pressed

I use VueJS component provided by 3rd-party library, which has its own events. I can handle such events as usual with "v-on" directive and specifying interesting event name, like that:
<TheComponent v-on:itemchanged="handler"/>
But is it possible to run handler only when 'itemchanged' occurs with CTRL key pressed? The handler should not run when CTRL key is not pressed.
I tried to play with click.crtl and keydown.ctrl but have no success.
Is it possible to run handler only when 'itemchanged' occurs with CTRL key pressed?
Assuming itemchanged is triggered by a change event: no, not without a workaround. Unlike keyboard and mouse events, change contains no keyboard info. You'll need to track the status of ctrl keypresses independently because JavaScript has no way of testing whether a key is down outside of those events.
Plugin
One encapsulated way to accomplish it is with a plugin:
const pluginKeypresses = function(Vue) {
const keyPresses = {
Control: false
}
window.onkeyup = function(e) { keyPresses[e.key] = false; }
window.onkeydown = function(e) { keyPresses[e.key] = true; }
Vue.prototype.$keyPresses = Vue.observable(keyPresses);
}
Vue.use(pluginKeypresses);
Note: The e.key detection here is the current standard as of 11/2020, but is not yet fully supported by IE and some other browsers. If IE matters to you, change the key detection implementation to your liking.
Usage
From anywhere in the app you can test this.$keyPresses.Control (boolean)
Demo
Don't you receive an event object with this event ? By adding a parameter to handler you should be able to catch the event, and check inside which key is pressed. Then just add a if statement at the beginning of you handler function.

Propagate wxWindows mouse events upwards

How can I arrange to propagate wxWindows right click mouse events up to the parent window from static text controls?
Motivation:
My GUI looks like this
Each box ( wxPanel ) represents a 'device' I want a right click on the box to pop-up a dialog to edit the device info.
This works fine so long as the user clicks on the background of the panels. If the click falls on any of the text ( wxStaticText ), then the panel never sees the click event. Since the text covers a big fraction of the device panels, this is unsatisfactory.
I understand the cause is that mouse events do not inherit from wxCommandEvent. The documentation states: by default only wxCommandEvent-derived events are set to propagate. But I do not see how to change the default behaviour so that the right click events are set to propagate. It seems that this should be possible because the docs also say other events can be propagated as well because the event handling code uses wxEvent::ShouldPropagate() to check whether an event should be propagated.
Maybe I need to need to specialize the wxStaticText class, so that it 'handles' the right click event, calling wxEvent::ResumePropagation( 1 ) on it?
Here is the device panel constructor
cDevicePanel::cDevicePanel( wxWindow* parent, devicify::cDevice& device )
: wxPanel( parent,-1,wxDefaultPosition,wxSize(200,110),wxBORDER_RAISED )
, myDevice( device )
{
switch( device.myMatch )
{
case devicify::cDevice::eMatch::OK:
SetBackgroundColour( wxColor(120,180,255) );
break;
case devicify::cDevice::eMatch::noDevice:
SetBackgroundColour( wxColor(255,180,120) );
break;
case devicify::cDevice::eMatch::noDetails:
SetBackgroundColour( wxColor(255,255,0) );
break;
}
myName = new wxStaticText(this,-1,device.myName,wxPoint(10,10));
myPower = new wxStaticText(this,-1,
wxString::Format("Power: %f kw",myDevice.myPower),
wxPoint(20,30));
myEnergy = new wxStaticText(this,-1,
wxString::Format("Energy: %f kwh",myDevice.myEnergy),
wxPoint(20,50));
myButton = new wxButton(this,ButtonDeviceOnOff,"OFF",wxPoint(10,70),wxSize(40,20));
Bind(wxEVT_BUTTON,&cDevicePanel::OnSwitch, this,ButtonDeviceOnOff );
new wxButton(this,ButtonDevicePlot,"Plot",wxPoint(90,70),wxSize(40,20));
Bind(wxEVT_BUTTON,&cDevicePanel::OnPlot, this,ButtonDevicePlot );
Bind(wxEVT_RIGHT_UP,&cDevicePanel::OnRightClick, this );
}
All this propagation stuff predates Bind() and maybe even Connect(). Now that we have those, the simplest solution is to just bind to the event you're interested in in each of the controls. It does mean that you need to do it for all the controls, but it's simple to iterate over all the children and you would need to do something for all windows with any other solution anyhow.
The "other events can be propagated" refers to custom events you could define, I think. I.e. if you're defining a new event class, you could set m_propagationLevel to wxEVENT_PROPAGATE_MAX for it to make it behave as wxCommandEvent.
As VZ suggests, it is easiest to ignore propagation issues and to solve the problem using bind.
Important:
Call bind as a member of the child window
Pass pointer to parent window as parameter to bind
So, my constructor looks like this:
myName = new wxStaticText(this,-1,device.myName,wxPoint(10,10));
myName->Bind( wxEVT_RIGHT_DOWN, &cDevicePanel::OnRightClick, this );
myPower = new wxStaticText(this,-1,
wxString::Format("Power: %f kw",myDevice.myPower),
wxPoint(20,30));
myPower->Bind( wxEVT_RIGHT_DOWN, &cDevicePanel::OnRightClick, this );
myEnergy = new wxStaticText(this,-1,
wxString::Format("Energy: %f kwh",myDevice.myEnergy),
wxPoint(20,50));
myEnergy->Bind( wxEVT_RIGHT_DOWN, &cDevicePanel::OnRightClick, this );
// right clicks on panel background
Bind(wxEVT_RIGHT_DOWN,&cDevicePanel::OnRightClick, this );

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

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:].

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.

The 10.6.3 os x update broke simulated key-presses for Nestopia

The iPhone app that I released is a wireless game controller, it translates touches on the device into key-presses on the networked Mac. This allowed for playing emulator (e.g. Nestopia) games using the iPhone as a controller. Of course, the day that I released it coincided with an os x update. After installing this update, the simulated key-presses no longer work in Nestopia! The crazier thing is, when I go to 'File > Open' within Nestopia, I can cycle through the file list by hitting the up-arrow on my iphone controller; i.e. the simulated key-presses work in menu items, but not in the game itself. The code that I use to simulate keys is below. Given the list of changes here, can anyone identify which change would cause this problem?
Thanks!!
#define UP false
#define DOWN true
-(void)sendKey:(CGKeyCode)keycode andKeyDirection:(BOOL)keydirection{
CGEventRef eventRef = CGEventCreateKeyboardEvent(NULL, keycode, keydirection);
CGEventPost(kCGSessionEventTap, eventRef);
CFRelease(eventRef);
}
The author of Mac Nestopia is using an older call, GetKeys(), to capture key events. As of 10.6.3, GetKeys does not catch generated key presses using the methods detailed in this post. The workaround I found was to use this instead:
-(void)sendKey:(CGKeyCode)keycode andKeyDirection:(BOOL)keydirection{
AXUIElementRef axSystemWideElement = AXUIElementCreateSystemWide();
AXError err = AXUIElementPostKeyboardEvent(axSystemWideElement, 0, keycode, keydirection);
if (err != kAXErrorSuccess)
NSLog(#" Did not post key press!");
}
Huge thanks to Richard Bannister for his quick email responses!
I think it's a problem with your code and not with 10.6.3. I have an app I'm writing that simulates key presses, and I've upgraded to 10.6.3, and my simulated key presses still work just fine.
Here's what I'm doing:
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef keyDownPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCode, YES);
CGEventSetFlags(keyDownPress, (CGEventFlags)flags);
CGEventRef keyUpPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCode, NO);
CGEventPost(kCGAnnotatedSessionEventTap, keyDownPress);
CGEventPost(kCGAnnotatedSessionEventTap, keyUpPress);
CFRelease(keyDownPress);
CFRelease(keyUpPress);
CFRelease(source);