nsevent

Send NSEvent to background app

时光怂恿深爱的人放手 提交于 2019-12-05 05:19:11
I need to send the key combo ^⌘C to a background app with the bundle identifier com.company.app . The key combo should then activate a menu item in that application. Unfortunately, I have no clue how to do that. Some research pointed me to the NSEvent and CGEvent API using CGEventPostToPSN() , but I was unable to get it work correctly, as I don't know how to set up the key combo. CGEventPost() didn't seem to work with the events I created, even if the desired app is the active one. Here is the code I eventually came up with but that doesn't work: CGWindowID windowNumber; NSEvent *event =

NSEvent addGlobalMonitorForEventsMatchingMask: Hotkey Intercepting

烈酒焚心 提交于 2019-12-04 18:02:42
I wanna intercept hotkeys that begin with Control + Shift and ends with a character (mandatory). I have the following code: [NSEvent addGlobalMonitorForEventsMatchingMask:NSFlagsChangedMask handler: ^(NSEvent *event) { NSUInteger flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask; if(flags == NSControlKeyMask + NSShiftKeyMask){ NSLog(@"pressed!"); } }]; What do i need to add to my code to check if the user pressed Control Shift +character, and what character the user pressed? The code NSLog(@"pressed!"); will be executed only if what i said above is true. This is my pseudo

How to monitor for swipe gesture globally in OS X

…衆ロ難τιáo~ 提交于 2019-12-04 14:10:30
I'd like to make an OSX application that runs in the background and performs some function when a swipe down with four fingers is detected on the trackpad. Seems easy enough. Apple's docs show almost exactly this here . Their example monitors for mouse down events. As a simple test, I put the following in applicationDidFinishLaunching: in my AppDelegate . void (^handler)(NSEvent *e) = ^(NSEvent *e) { NSLog(@"Left Mouse Down!"); }; [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask handler:handler]; This works as expected. However, changing NSLeftMouseDownMask to

How to pass scroll events to parent NSScrollView

a 夏天 提交于 2019-12-04 05:09:48
I need fixed-size NSTextViews inside a larger scrolling window. IB requires that the textviews be inside their own NSScrollViews, even though their min/max sizes are fixed so that they won’t actually scroll. When trackpad gestures are made within the textview frames (regardless of whether they have focus), they are captured by the textviews’ scrollviews, so nothing happens. How do I tell the textviews’ scrollviews to pass scroll events up to the window’s main scrollview? (Or perhaps I should be asking how I tell the window’s main scrollview to handle these events itself and not pass them on to

macOS App: handling key combinations bound to global keyboard shortcuts

爷,独闯天下 提交于 2019-12-03 08:15:46
In some apps, it makes sense for the app to directly handle keyboard shortcuts which are otherwise bound to system wide combinations. For example, ⌘-Space (normally Spotlight) or ⌘-Tab (normally app switcher). This works in various Mac apps, such as VMWare Fusion, Apple's own Screen Sharing and Remote Desktop clients (forwarding the events to the VM or server, respectively, instead of handling them locally), and also some similar third-party apps in the App Store. We would like to implement such a mode in the app we're working on, but are having a hard time working out how to do it. I should

macOS Key Event Slow Repeat

社会主义新天地 提交于 2019-12-02 18:37:45
问题 I'm trying to create a small WASD demo game in macOS. I'm using NSEvent for handling the key events. To detect the key presses, I'm searching for keyDown events. Here's what I have: NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (keyEvent) -> NSEvent? in if self.keyDown(with: keyEvent) { return nil } else { return keyEvent } } func keyDown(with event: NSEvent) -> Bool { userInt.keyDown(key: event.characters) return true } So here, I'm holding the keys down (as you'd expect in a game),

macOS Key Event Slow Repeat

烈酒焚心 提交于 2019-12-02 09:43:31
I'm trying to create a small WASD demo game in macOS. I'm using NSEvent for handling the key events. To detect the key presses, I'm searching for keyDown events. Here's what I have: NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (keyEvent) -> NSEvent? in if self.keyDown(with: keyEvent) { return nil } else { return keyEvent } } func keyDown(with event: NSEvent) -> Bool { userInt.keyDown(key: event.characters) return true } So here, I'm holding the keys down (as you'd expect in a game), and I'm getting some very slow movement. Like, when I'm holding it down, it's very janky. Upon further

Detect any key press, including modifier keys, on OS X

爱⌒轻易说出口 提交于 2019-12-02 01:21:16
I need to detect all the keys that the user presses. Using -keyDown: I can get most key presses (alphanumeric, function keys, arrows, space bar, escape, return), but I cannot get any modifier key when it's pressed alone. How do I detect absolutely any keystroke, including modifier keys? Try it: [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask|NSFlagsChangedMask handler:^NSEvent *(NSEvent *incomingEvent) { if (incomingEvent.type == NSFlagsChanged && (incomingEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) { NSLog(@"modifier key down"); } else if (incomingEvent.type ==

Detect left and right mouse click on NSView

北城余情 提交于 2019-12-01 03:53:43
In Swift: I created a simple NSView and now want to execute different functions, depending on which mouseButton is pressed (left or right). how can I detect this? Warren Burton You trap the corresponding mouseDown events import Cocoa class MyView : NSView { override func mouseDown(theEvent : NSEvent) { println("left mouse") } override func rightMouseDown(theEvent : NSEvent) { println("right mouse") } } See NSResponder for more magic. Swift 4 import Cocoa class MyView : NSView { override func mouseDown(with theEvent: NSEvent) { print("left mouse") } override func rightMouseDown(with theEvent:

Detect left and right mouse click on NSView

自作多情 提交于 2019-12-01 00:36:26
问题 In Swift: I created a simple NSView and now want to execute different functions, depending on which mouseButton is pressed (left or right). how can I detect this? 回答1: You trap the corresponding mouseDown events import Cocoa class MyView : NSView { override func mouseDown(theEvent : NSEvent) { println("left mouse") } override func rightMouseDown(theEvent : NSEvent) { println("right mouse") } } See NSResponder for more magic. Swift 4 import Cocoa class MyView : NSView { override func mouseDown