nsevent

Register a global hotkey without support for assistive devices enabled

末鹿安然 提交于 2019-12-22 21:55:58
问题 Using this code, I may register a global event handler: [NSEvent addGlobalMonitorForEventsMatchingMask: NSKeyDownMask handler: ^(NSEvent *incomingEvent) { NSString *chars = [[incomingEvent characters] lowercaseString]; unichar character = [chars characterAtIndex:0]; // do something useful NSLog(@"keydown globally! Which key? This key: %c", character); }]; Unfortunately, events get passed along to this monitor, if support for assistive devices is enabled. Without assistive devices being

Send NSEvent to background app

一笑奈何 提交于 2019-12-22 04:56:16
问题 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

NSEvent addGlobalMonitorForEventsMatchingMask: Hotkey Intercepting

心不动则不痛 提交于 2019-12-21 22:32:34
问题 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

Scrolling in NSScrollView stops when overwriting scrollWheel function

天涯浪子 提交于 2019-12-20 03:12:30
问题 I experience a weird bug when I overwrite the scrollWheel(theEvent: NSEvent) function in a NSScrollView. Here's my code: import Cocoa class GraphScrollView: NSScrollView { var axisScrollViewInstance: AxisScrollView? override func scrollWheel(theEvent: NSEvent) { if theEvent.deltaY != 0 { axisScrollViewInstance?.scrollEventFromOtherScrollView(theEvent) super.scrollWheel(theEvent) } else if theEvent.deltaX != 0 { super.scrollWheel(theEvent) } } } class AxisScrollView: NSScrollView { var

Mouse Down events in Objective-C

风格不统一 提交于 2019-12-20 01:46:22
问题 I know this question has been asked a lot before, but nothing will work for me. The following code will not do anything at all. - (void) mouseDown:(NSEvent*)event { NSLog(@"It worked!"); } I have tried a lot of different methods to get this to work, including creating custom NSEvents in this way: NSEvent *someEvent; - (void) mouseDown:(NSEvent*)someEvent { NSLog(@"It worked!"); } This is my .h file: @interface test : NSWindow <NSWindowDelegate> { } Would somebody explain how to make this do

Mac Cocoa: How to differentiate if a NSScrollWheel event is from a mouse or trackpad?

[亡魂溺海] 提交于 2019-12-18 16:49:30
问题 In my application, I want the scrolling to happen, only with scroll wheel action from a mouse and not from the two finger gesture on a trackpad. Basically, I am trying to determine if the scrollWheelEvent is generated from the mouse or trackpad, inside - (void)scrollWheel:(NSEvent *)theEvent method. From what I know so far, it seems like there is no straightforward way to accomplish this. I tried a work around of setting a boolean variable to true and false inside -(void)beginGestureWithEvent

Check modifierFlags of NSEvent if a certain modifier was pressed but no other

心已入冬 提交于 2019-12-18 11:49:56
问题 I just experimented with the addLocalMonitorForEventsMatchingMask:handler: method in NSEvent and came across the following question: How do I find out if only certain modifiers were pressed? A short example to set this question into context: I wanted to listen for the shortcut "⌘+W". Therefore I wrote the following code: [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *theEvent) { if ([theEvent modifierFlags] & NSCommandKeyMask && [theEvent keyCode] == 13) {

Cocoa - NSEvent Respond to the SHIFT key?

一笑奈何 提交于 2019-12-18 06:58:11
问题 I was wondering if NSEvent responds to the "Shift" key on the keyboard. I am logging the keyCodes when debugging my app and I don't get a keyCode value for the shift key. Thanks, Kevin EDIT: This is the code I am using from a user response. -(void)keyDown:(NSEvent*)event { if ([event modifierFlags] == NSShiftKeyMask) { NSLog(@"Shift key pressed"); } } The Shift key is still not being recognized... 回答1: Take a look at the flagsChanged: method of NSResponder . Something like this: - (void)

How to store (and use) the current mouse position?

时光毁灭记忆、已成空白 提交于 2019-12-13 16:15:51
问题 What is the best way to store the current mouse position (system-wide) and then (later) put the mouse at that stored point? [NSEvent mouseLocation] gets me the position, and I can move the mouse with a CGEventMouseMoved, but they each use a different co-ordinates system (I believe y=0 is the top for NSEvent and the bottom for a CGEvent). I'm worried about the robustness of capturing the screen height and using it to convert between the two - or is this the best approach? 回答1: Yes, using the

Position of mouse click relative to scene, not window

有些话、适合烂在心里 提交于 2019-12-12 12:48:07
问题 In my game I need to use the mouse to select units. However, I am encountering problems as I do not know how to get the coordinates of the click relative to the game, not to the window. For example, if Unit 1 is at the (0,0) point of the game, it could be at any point on the window depending on how I pan and zoom the window, but I want the mouse click to return (0,0) no matter how I move the window as long as I click on the same spot. Right now I am using: override func mouseDown(with event: