cgeventtap

How do I process a mouse click using Quartz events?

偶尔善良 提交于 2020-02-23 04:06:19
问题 I have a CGEventTap and I would like to observe and process all system mouse click events. Detecting a mouse click event is easy, but I don’t want some of the clicks to get processed by the rest of the system. And I can’t figure out how. The system only sends low-level events like mouseUp , mouseDown , mouseDragged and so on. When I receive a mouseDown event, I don’t know if it’s a part of a mouse click yet, could be a drag event. I have to wait for the following event – if it’s a mouseUp , I

Virtual keypress goes to wrong application

我怕爱的太早我们不能终老 提交于 2020-01-05 15:14:54
问题 I have the following code to send virtual keypresses to a process given its pid NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: pid]; [app activateWithOptions: (NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)]; event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)cg_key_code, true); event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)cg_key_code, false); CGEventPost(kCGHIDEventTap, event1); CGEventPost(kCGHIDEventTap,

Can't simulate backspace button on some applications with event tap

≡放荡痞女 提交于 2019-12-24 14:37:08
问题 I'm trying to simulate some key presses on Mac OS. This code is supposed to delete one previous character if the 'h' key is pressed (e.g. if user types 'tigh' it will become 'ti') by modifying keyboard events. However it only works with some applications; others totally refuse my events. Is there any problem with this code ? - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { CFMachPortRef eventTap; CGEventMask eventMask; CFRunLoopSourceRef runLoopSource; eventMask = ((1 <

CGEventTap blocks application input

浪子不回头ぞ 提交于 2019-12-22 08:24:48
问题 I'm trying to use CGCreateEventTap to monitor global mouse clicks, however when I do this it seems to block interaction with my own app. Mouse clicks in other running apps work fine, but my own app (that is the DemoAppDelegate app) does not respond completely. I can drag the main window for the app, but the red/yellow/green window buttons are greyed out. And the DemoApp's menu is unclickable as well. This seems really strange to me, and I've been unable to figure it out. Examples of using

Simulating OPT (or other modifiers) with `CGEventCreateKeyboardEvent` `CGEventSetFlags` `CGEventPost`

谁都会走 提交于 2019-12-13 03:38:31
问题 I'm trying to simulate key-down and key-up for the OPT key on macOS. CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateHIDSystemState ); CGEventRef ev = CGEventCreateKeyboardEvent( source, keycode, is_down ); // CGEventSetFlags( ev, NSEventModifierFlagOption ); CGEventPost( kCGHIDEventTap, ev ); CFRelease( ev ); ... with is_down = true then false . It works in most situations. However, It does not match the native behaviour if I perform the following test: I hover over a

How to consume CAPS-LOCK event on MacOS

戏子无情 提交于 2019-12-11 17:22:50
问题 I am trying to remap CAPS-LOCK. I can successfully detect CAPS-LOCK key-down/up events using CGEventTap . However, I am unable to consume them. (OTOH I am able to consume ordinary key-down/up events). The below code excerpt illustrates this: @implementation Intercept - (BOOL) tapEvents { _eventTap = CGEventTapCreate( kCGHIDEventTap, // kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMaskBit( NSEventTypeKeyDown ) | CGEventMaskBit( NSEventTypeFlagsChanged ) |

How to post a Quartz Event after Swift application launch?

孤者浪人 提交于 2019-12-11 07:41:40
问题 I am writing a simple Cocoa app that will be launched from AppleScript just to post a Quartz Event to another app. There is no need for a user interface so I deleted the window from the Interface Builder and the outlet to it from the Application Delegate. I call a private method postClickEvent() from the method applicationDidFinishLaunching(_:). I initialize leftMouseDown and leftMouseUp CGEvents with nil mouseEventSource at mouseCursorPosition (x: 0, y: 0) and post them at location

Simulate/Toggle CAPS LOCK programatically in OS X

断了今生、忘了曾经 提交于 2019-12-11 07:05:54
问题 I have seen many post on this topic. But haven't found a clear answer anywhere. Is there a way to toggle CAPS LOCK in Objective-C or C code? I am not looking for a solution using X11 libs. I am not bothered about the LED on/off status. But just the functionality of CAPS LOCK (changing the case of letters and printing the special characters on number keys). Why is CGEvent not supporting this the way it does for other keys? 回答1: var ioConnect: io_connect_t = .init(0) let ioService =

Cocoa: Simulating Command+Tab in CGEvent

懵懂的女人 提交于 2019-12-10 13:55:43
问题 I want to simulate Application Switcher in my app and I think CGEvent maybe can do that. Well, after learning some basic information about CGEvent, I can simulate the key press Command + Tab . But the Application Switcher window just flashing by and switch to another app immediately. I realize that I need to hold the Command key and press Tab key to choose the app. So, here's my code: // Hold the Command key let source = CGEventSourceCreate(.HIDSystemState) let event =

Event taps: Varying results with CGEventPost, kCGSessionEventTap, kCGAnnotatedSessionEventTap, CGEventTapPostEvent

安稳与你 提交于 2019-12-07 10:47:06
问题 I'm running into a thorny problem with posting an event from an event tap. I'm tapping for NSSystemDefined at kCGHIDEventTap, then replacing the event with a new one. The problem I'm running in to is that depending on how I post the event, it's being seen only by some applications. My test applications are Opera, Firefox, Quicksilver, and Xcode. Here are the different techniques I've tried within my event tap callback, with results. I'm expecting an action (the "correct response") from each