224 questions
0
votes
2
answers
61
views
Invisibly block user interaction in macOS app
In my macOS app (objC), the user can trigger tasks that can take some time, like importing files into a database. These tasks do not run on the main thread because they spawn a window (sheet) to show ...
1
vote
0
answers
86
views
How to use Java's FFM API to call NSEvent.addGlobalMonitorForEventsMatchingMask on macOS?
I try to use Java's FFM API to call NSEvent.addGlobalMonitorForEventsMatchingMask on macOS.
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
...
0
votes
1
answer
60
views
NSCollectionView type select
I have implemented type selection for my NSCollectionView implementation.
In my collection view subclass, I have added additional delegate methods:
protocol CollectionViewDelegate : ...
0
votes
0
answers
244
views
Adding a global monitor with NSEvent.EventTypeMask mask does not trigger
I'm developing a Mac application for the menu bar to manage a list of values copied via command + c. I would like my app to perform actions when I press that key combination, even when it is not ...
-1
votes
1
answer
96
views
AppKit Reading Keyboard Input in Separate Thread
Is there anyway to read keyboard inputs in AppKit off the main thread? I've managed to do so using CGEventTapCreate but unfortunately it requires permissions I'd rather not have to request. Is there a ...
1
vote
1
answer
91
views
How to add WASD movement in Cocoa Swift
So I am trying to add WASD movement to my playCharacter class in Cocoa Swift (not using Xcode):
import Cocoa
public class playerCharacter: NSView {
var x: Int
var y: Int
let color: ...
1
vote
0
answers
177
views
How to get double click interval on MacOS
We're developing a Unity editor extension. For that we need to know the double click interval of the OS.
On windows you can get this with an external call like so
(how to get the double-click-time in ...
1
vote
0
answers
358
views
How to post to event to specific window on MacOS?
I am trying to post a command + mouse drag event to a specific window.
Create an NSEvent
Post the underline cgevent
It does not work whether with postToPid or post. What I want to achieve is that: ...
0
votes
0
answers
218
views
Is there any way to NSEvent convert to string array or string object?
This is the code I tried.It gives the mouse Click event in macOS
let eventObserver = NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.leftMouseDown) { (event) -> Void in
Swift....
0
votes
2
answers
322
views
CGEventPost doesnt work in compiled mode on MacOS 12.6 but works in interpreted mode
I'm trying to programmaticaly send a custom key event (function keys, media keys) but it only works in interpreted mode, not in compiled code.
I tried using the following answer:
emulate media key ...
0
votes
1
answer
68
views
Diagonal moves with the keyboard in macOS
My son noted that he could not use the cursor keys to move in a simple game I wrote. I followed the guide found here on SO, and now you can move the four cardinal directions. But the game allows eight ...
0
votes
2
answers
1k
views
How to detect a single mouse down event in SwiftUI on macOS
I want to detect ONE mouse down and ONE mouse up event on my view (a simple Rectangle). Here is the code I already made. Unfortunately I got a lot of 'mouse down' and 'mouse up' on the console. This ...
0
votes
0
answers
231
views
Quartz CGEvent mouse click not working anymore
I made a command line tool years ago that worked fine up until macOS Monterey. The code below no longer works for a command line tool. It works fine if I use a macOS application. I need a command line ...
1
vote
1
answer
388
views
Listening for click/touch events in SKScene on MacOS in a SwiftUI SpriteView
I'm trying to intercept mouse click events inside SpriteKit SKScene inside a a SwiftUI SpriteView in Mac OS. Overriding touchesBegun() that accepts an NSEvent object is never called when I click ...
0
votes
0
answers
91
views
Fetching Global Events and Notifications in command line application with swift
I have a command line application and I would like to receive global mousemove and keypress events as well as some notifications. However this will not work. The console Application does have ...
0
votes
0
answers
233
views
How to show NSPopover without dismissing the first responder?
I'm working on a mac app that has similar functionalities to the rocket app.
Whenever the user types : outside of my app I'll show an NSPopover with NSTableView near the cursor.
The problem I'm facing ...
-1
votes
1
answer
635
views
How to get key code or key text from an NSEvent in AppleScript with AppKit
I would like to be able to get either the key code or the characters/text from an NSEvent/KeyEvent. Absolutely no idea how to do this as the documentation I have found so far has been very limited.
My ...
0
votes
0
answers
138
views
AppKit prevent tracking area updates
Some built in AppKit components temporarily "pause" NSTrackingArea updates while their content is being scrolled or resized. For example, if you inspect tracking areas using the Quartz Debug ...
4
votes
1
answer
295
views
Keep mouse within area in Swift
I'm trying to prevent the mouse cursor from leaving a specific area of the screen. I can't find a native method to do this, so I'm trying to do it manually.
So far I have this:
NSEvent....
1
vote
1
answer
1k
views
m1 mac, python assertion failure and crashes when using osx as backend in matplotlib & jupyter notebook
The error occurs when I was using jupyter notebook
I am using a m1 macbook pro with monterey 12.1, lastest version as this is being written
Here is my code
import matplotlib.pyplot as plt
%matplotlib ...
3
votes
2
answers
583
views
Is it possible to disable "funk" error sound on global keyboard events in SwiftUI (macOS)?
I've started a SwiftUI project (it is a macOS tray application) that relies on global keyboard events (even when my application is minimized). Specifically i care about the F3 and F4 keys. While the ...
1
vote
0
answers
92
views
NSEventMaskCursorUpdate with addGlobalMonitorForEventsMatchingMask not triggering
I am having trouble getting cursor updates with addGlobalMonitorForEventsMatchingMask and NSEventMaskCursorUpdate.
I was hoping this handler would be called when the (global) mouse cursor changes, but ...
0
votes
0
answers
497
views
Covert NSView's mouse down coordinates to screen coordinate system in cocoa Swift
override func mouseDown(with event: NSEvent) {
let startPoint = event.locationInWindow
print(startPoint) //for top left it prints (0, 900)
To my knowledge the screen's origin is ...
2
votes
2
answers
2k
views
Detect command key press in macOS Swift [duplicate]
I am using event monitor to detect the key press event in swift. However, event monitor doesn't seem to detect command key or any other modifier key (shift, tab, opt ...) press. Is there a different ...
0
votes
1
answer
495
views
Key down event handler running multiple times in Swift
I am trying to do something when a given key is pressed in a macOS App. First, I ran into a problem where the keyDown event was detected multiple times on each press, therefore executing the handler ...
3
votes
1
answer
572
views
Make keydown NSEvent run once in Swift
NSEvent keydown code is running repeatedly when key is pressed. How do I make sure that the event runs only once then stop listening until key up event occurs?
override func viewDidLoad() {
...
2
votes
1
answer
991
views
Stop listening to NSEvent in Swift
I am trying to get NSEvent to stop listening to local event. Is there a way to access the event after creating it?(which is what I unsuccessfully tried in removeEvent)
override func viewDidLoad() {...
1
vote
1
answer
247
views
Change contentTintColor on mouseDown for NSButton
I have a custom button that subclasses NSButton. I want to change the content tint color when the button is in pressed state. This is what I have:
open override func mouseDown(with event: NSEvent) {
...
0
votes
2
answers
564
views
How to run custom code in the main thread of Cocoa applications
I'm a windows developer and I'm having a hard time understanding the right way to run code in the
NSApplication's main thread.
Most of my code is running in a cvdisplaylink thread (it's an opengl ...
0
votes
1
answer
56
views
Bad access in certain Obj-C Functions
So I am working on a sort of GLFW-lite framework and am doing the cocoa part. I am trying to get the input working but it is not working. Basically, my view interface has a pointer to a struct with an ...
1
vote
1
answer
2k
views
How to detect Caps Lock status on macOS with Swift without a window?
I have tried KeyDown and NSEvent, but they require a NSWindow object to be active. My hope is that I can put an app on the status bar and alert the user when it has pressed CapsLock, even if the user ...
0
votes
0
answers
31
views
How to capture event from apple remote?
I want to capture events from an apple remote in a macOS App.
I tried to use EventTap to capture CGEvent from the system and found it do send an event. Then I turn it into NSEvent, then found the ...
0
votes
0
answers
579
views
Is there any documentation for the `NSEventTypeSystemDefined ` `NSEvent`?
Apple provides https://developer.apple.com/documentation/appkit/nseventtype/nseventtypesystemdefined?language=objc
This page says nothing more than:
No overview available.
Declaration:
...
1
vote
1
answer
392
views
Overriding keyDown in NSWindowController with NSTableView | responding to spacebar
So I have a NSTableView inside NSWindowController. When I press spacebar it goes through NSResponder and stops on NSWindow which calls performClick: on the NSTableView (NSControl)
All other key events ...
1
vote
1
answer
231
views
Why is this functionKey selected when I press F1, F2, etc?
I am reading the keyboard shortcuts typed inside a NSTextField.
When I press, for example, shift + option + F2, this method
class func convertToString(event:NSEvent) -> String {
var shortcut = ""...
0
votes
1
answer
270
views
Inject keyboard event into NSRunningApplication immediately after foregrounding it
I am trying to bring to foreground a NSRunningApplication* instance, and inject a keyboard event.
NSRunningApplication* app = ...;
[app activateWithOptions: 0];
inject_keystrokes();
... fails to ...
0
votes
1
answer
299
views
Is it safe to create `NSEvent object` then `CGEventPost` the `[ev CGEvent]` then exit the method?
I have the following Objective-C snippet:
void toggle()
{
NSEvent* down_event = [NSEvent keyEventWithType: NSEventTypeKeyDown
location: NSZeroPoint
...
0
votes
1
answer
321
views
How press "shift" + "command" + "3" simultaneously programmatically
I am trying to capture a screenshot by posting this Keyboard event. I tried below way which doesn't work:
1]
CGEventRef event1, event2, event3, event4, event5, event6;
event1 = ...
1
vote
1
answer
1k
views
NSEvent leak for key down in macOS
In Xcode 10.1 with Swift 4.2 I'm having a memory leak when I add a local monitor for key down events in my NSViewController, that it is be instanced as minimal version (without nib and xib).
override ...
0
votes
0
answers
225
views
Local NSEvent Monitor yields incorrect keycode
I'm using a local monitor to observe key events. However, I get an incorrect keycode when the modifiers ⌥ and ⌘ are active and the key with code 42 is pressed simultaneously. The keyCode in the event ...
3
votes
1
answer
491
views
How to judge NSEvent from trackpad click, not tap to click
When "tap to click" checked, how judge NSEvent is from trackpad click(pressed down) or tap to click.
- (void)mouseEvent:(NSEvent*)theEvent {
if ((type == NSLeftMouseUp || type == NSLeftMouseDown) &...
3
votes
1
answer
2k
views
NSEvent addLocalMonitorForEventsMatchingMask Memory Leak
Here again with another memory leak issue. I have an NSStatusItem that I'm trying to capture mouse clicks on and react. The following code causes Instruments to report a leak and removing this code ...
0
votes
1
answer
107
views
touchesBegan not called in NSScrollView subclass until the first mouse/trackpad click on the view
I subclass an NSScrollView to monitor 2-finger panning with touchesBegan/Moved/Ended. It works as long as I click on the view once after the app is launched. Then it will always work till the next ...
0
votes
0
answers
49
views
Error in capturing cmd + in Swift
I am trying to capture cmd - and cmd + key presses in Swift, cmd - works fine but when pressing cmd + I get an error bong sound.
override func keyDown(with event: NSEvent) {
if event....
-1
votes
1
answer
169
views
How to return an undefined value for NSPoint
I am learning swift and I am trying to make a simple app where I move an object (The default ship) with the keyboard. I am designing an event to represent the left keypress. Since I do not need the ...
0
votes
1
answer
450
views
macOS Key Event Slow Repeat
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....
1
vote
0
answers
374
views
Ordering of events from addGlobalMonitorForEvents
I am trying to monitor mouse click events like so:
clickMonitor = NSEvent.addGlobalMonitorForEvents(
matching: [.leftMouseDown, .rightMouseDown, .leftMouseUp, .rightMouseUp]) {
[unowned self] ...
1
vote
0
answers
374
views
mouseExited is no longer called after mouseDown in NSButton subclass
In building a custom NSButton, I've run into a problem handling highlight behavior. After clicking down on the button, holding, and dragging the cursor outside the button's bounds, mouseExited: and ...
-1
votes
1
answer
183
views
Tracking Duration of Mouse Event
Is there a way to track the time between a mouseDown and a mouseUp event in swift? Or would one have to implement their own tracker manually?
5
votes
1
answer
3k
views
Adding a global monitor with NSEventMaskKeyDown mask does not trigger
I'm working on a MacOS menu bar app which needs to track some global shortcuts in order to facilitate adjusting display brightness on external monitors. However, I cannot get it to fire a handler on ...