How do I Capture keyboard/mouse events with my application in focus? - objective-c

I'm building a synergy like app for Mac OS, that captures keyboard/mouse and sends them to a remote computer.
I wish to capture all user keyboard and mouse events, while my NSWindow is in focus (if possible, while not in focus would be nicer). the catch is - I don't want system shortcuts like CMD+Tab or CMD+Q to interrupt me, I wish to handle them before the windowing system does, so that my app won't loose focus. Same for mouse.
Thanks

Check this - Cocoa Event-Handling Guide
Hope this helps :)

Related

Is Desktop Mode Accessible When Using Bluetooth Keyboards in ChromeOs v69?

I just found a great bluetooth keyboard by Brydge for the Pixel Slate and am wondering if current ChromeOs devices can manually transfer from "tablet" to "desktop" mode when you attach a bluetooth keyboard?
It seems like this is feasible by enabling a setting in developer mode. Thanks to #Skawtnyc's response on Reddit!
You can switch back and forth on the fly. Enable chrome://flags/#ash-debug-shortcuts and then you can use CTRL+SHIFT+ALT+T to switch back and forth between desktop and tablet mode.
If you connect a mouse it will automatically switch to desktop mode, but using this method you can still switch back and forth.
#Skawtnyc also points out a bug to watch out for when using an external display such as a monitor.
Keep in mind that when connected to a dock with an external display, this setup can trigger a crash/restart error. The cause is the virtual keyboard. Anything that causes the virtual keyboard to appear on screen will trigger it. The workaround is to go into Accessibility settings and turn off the on-screen keyboard. You don't need it anyway when using a physical keyboard.

How to prevent/change system-wide Keyboard / Mouse events under Mac OS X?

I find two method to handle global events on Mac OS:
NSEvent.addGlobalMonitorForEvents only get a copy of origin event, can monitor, but not change or prevent.
Use InstallEventHandler like the answer at How to Capture / Post system-wide Keyboard / Mouse events under Mac OS X? question. But only GetEventMonitorTarget() work, so I only monitor events。
So, How to prevent/change the system-wide events ? The KeyboardCleanTool app (http://blog.boastr.net/?p=2452) can do like that, so I think it is possible.
You can use Quartz Event Taps for that. In order to monitor/change/block keyboard events, your process will need to be trusted for Accessibility access.

Polling the keyboard in Cocoa

My Cocoa application should react continuously to the state of the keys pressed by the user. To that end, I would like to poll the keyboard at fixed intervals, instead of relying on keyboard events. Is there any Cocoa API to achieve this? If not, what other options exist?
Polling is never such a great idea. You shouldn't have to do that in most cases.
There is 2 API you may be interested for:
Quartz Event Taps https://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html
IOKit HID API https://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/HID/intro/intro.html#//apple_ref/doc/uid/TP40000970
Please note keyboard accesses may pose some security issues so the system may not allow you to read the keyboard state. Check the Accessibility settings (access to assistive devices).
Don't poll the keyboard, just implement -keydown: in your App delegate class.

How to detect external keyboard connectification in objective-c?

My application need to know are external keyboard connect or no. How could i do to know that? No Private API please. :)
If this is iOS related, I'm not sure why you'd want to detect this as the hardware keyboard acts exactly the same as the software keyboard (except with a few extra shortcuts for sound, brightness etc. which your app shouldn't use anyway).
If you're considering screen space, then the software keyboard will still send it's notifications when it is displayed or when it is hidden, so you can respond to those. For example, say you have the software keyboard up and then you connect your external keyboard. The software keyboard will hide, and will post the UIKeyboardWillHideNotification. So you can respond to that.

How to be notified in OS X when a drag operation *starts* anywhere?

I'm wondering if there's any way to have my application be notified when a drag-and-drop operation starts anywhere on the screen, even if I don't have an active window there.
I've looked into the normal drag-and-drop APIs, but I haven't spotted anything that does this. The NSDraggingDestination protocol along with the -[NSWindow/NSView registerForDraggedTypes:] method allows you to notice when someone is dragging something and that crosses over into your window, but I'd like to notice it when any dragging operation is started anywhere on the screen.
Any tips on how to go about this? Is there a standard Cocoa API for it, or is there a private API / some kind of dirty hack to get this information?
Thanks in advance :)
Take a look at NSEvent’s +addGlobalMonitorForEventsMatchingMask:handler:. I’m not sure if you can track mouse dragging but it’s certainly possible to track mouse button up/down events.
i haven't done it,
but i am assuming you need some kind of external software monitoring ALL mouse activity on the system, and reporting it to your app (or your application doing this itself),
as dragging events are usually reported in your app only when there is activity inside your app's window..