Can windows detect when a monitor, mouse or keyboard is disconnected from the computer? If so what Win32 API is used for this?
Not sure about the monitor, but the keyboard and mouse being disconnected should fire off a
WM_DEVICECHANGE message if they are USB devices. See this link for details: WM_DEVICECHANGE
There is a good discussion of WM_DEVICECHANGE on this forum, relating to its usage with HID devices (Mouse/Keyboard/etc.)
For more detailed notification you can use the RegisterDeviceNotification function
RegisterDeviceNotification
To react to monitor changes, handle WM_DISPLAYCHANGE (see http://msdn.microsoft.com/en-us/library/dd145210%28VS.85%29.aspx).
As far as I recall there is no way to detect PS/2 disconnection events. Monitor and USB can be trapped but I don't know how.
Related
I'm looking for the Mac OS API that virtual machine or remote desktop type programs would use to "capture" the mouse and keyboard. That is to say, I want to write a GUI program where when the user clicks in my window, the normal mouse cursor disappears, and all keyboard and mouse input is diverted to my program, including global shortcuts like cmd-tab. What's the name of this API?
Found it: CGEventTapCreate can tap into the low level event stream to receive, filter, or insert HID events.
I have a Windows tablet in my car. It is connected to the car via Bluetooth, so I can skip tracks, change volume, play/pause, and so on using my steering wheel. I want to fire an event depending on which button was pressed.
So far I've tried intercepting inputs and keypresses globally using:
NodeJS (using a library called iohook)
Listens for keypresses on the system, so I can always listen for keypresses (basically a keylogger)
Works great if I hit play/pause on the keyboard
Does not work if I hit play/pause over Bluetooth using a speaker or my car's steering wheel
.NET Windows Forms
"Keylogger" works great, but same problem with NodeJS: It does not listen for "keypresses" from a Bluetooth device
I don't care which language I have to use. I know that it's possible, because if I have Spotify running on my Windows machine and I hit play/pause on my speaker or on my car's steering wheel, it will pause Spotify. So something is definitely listening to the inputs, but I haven't figured out what.
One very important thing: It has to listen globally for the Bluetooth events. I do not want to have an application focused.
What language can I use, that can listen for these events? Are there frameworks or libraries out there, that can do this easily already?
I've been successfully using CG mouse events to simulate mouse down/drag/up events using a specialized hardware controller. However, I come across some applications in which using these CG mouse events has no effect- that is, I can click and drag the actual mouse to change controls within a certain area of the application, but simulating these exact same movements using CGCreateMouseEvent (tried posting to HID system state and CombinedSession state) does not work.
Perhaps these apps are listening specifically for a mouse/touchpad hardware device? Is there any way to more "realistically " simulate mouse events so that these app think the actual mouse/touchpad is dragging?
I am trying to understand how Linux kernel handles the mouse interrupts in detail.
I was able to trace out the flow starting from the interrupt in i8042.c to an event in mousedev.c.
In the function mousedev_key_event() in line#230 mousedev.c for Linux kernel 3.6,
I see that button events are decoded. But I am curious to see how is a double click event handled.
I tried looking for a time interval in which the second click has to be detected. I did find one for touchpad which is "tap_time" defined in the same file line#51, but not for an external PS/2 mouse. Can someone help me figure it?
Is there some event or notification I can receive or hook each time an external LCD monitor is plugged in or unplugged from a laptop running Windows 7?
The laptop detects this and switches my display to the external screen and back with certain kinds of resizing or repositioning but is this exposed by the operating system so that applications can provide a handler, attach a script, etc?
If not, is there a registry setting or API I could poll from time to time?
(I prefer programming C + Win32 API)
UPDATE
Mike's answer below, WM_DEVICECHANGE led me to RegisterDeviceNotification(), but I'm struggling to implement it so far...
UPDATE 2
This question has been asked with different wording a couple of times, but not fully answered yet in my opinion:
How to detect hot plugging of monitor in a win32 application?
Getting an event on monitor hotplug for windows
According to this article Windows sends the WM_DISPLAYCHANGE message when display resolution changes and also when a display is added or removed.
If you need to react to desktop size changes due to monitor addition or removal, you can do so in the handler of this message. The LPARAM gives you the new resolution of the display on which the window is located. Notice that this resolution will be scaled if you use anything else than 100% for system DPI scaling and your program is not DPI-aware.
Alternatively use the EnumDisplayMonitors function to get the display resolution for each connected monitor and the relative positions of the monitors in the virtual desktop. This functions uses the real device pixel values regardless of DPI scaling.
You can try WM_DEVICECHANGE. If that doesn't do the trick, run your window and attach Spy++ to it which will log all the window messages it receives. Then plug your monitor in and check if you received any messages.
Alternatively you can poll GetSystemMetrics() with SM_CMONITORS.
As said here:
You will see registered messages "UxdDisplayChangeMessage" and "HotplugDetected" (second one only when adding monitor). You can use RegisterWindowMessage to get the identifier for these messages.
There are also other messages you can check, just see the linked answer.