Globally identify last time modifier key was pressed - winapi

I have a bit of an odd question pertaining to Windows: is there any way to globally determine the last time that any key, or in particular, the modifier key (aka alt) was pressed without resorting to drivers or kernel-mode code?
Some background: I have registered a global shortcut (alt+`) and successfully run code when it is executed. Currently, I use some heuristics that are very much fallible to determine if the user is repeatedly pressing and releasing the backtick key while the alt key is consistently held down or if the user has pressed and released both since the last time my hotkey handler was called.
I wish to more-precisely ascertain whether or not the alt key has been released since the last time my handler was called. Being able to uniquely identify the time of the last alt keypress is an obvious solution. Another is somehow hooking on to each alt key press to record that info, which I do not believe is possible.
I'm open to all ideas and suggestions!

You can install a global low-level keyboard hook (by calling SetWindowsHookEx, passing a LowLevelKeyboardProc). This allows you to globally monitor the WM_KEYUP event for the VK_MENU, VK_LMENU and/or VK_RMENU key. Together with the timestamp recorded in the hotkey handler you can determine, whether the hotkey is part of the same Alt sequence or a new one: If the timestamp of the WM_KEYUP event is larger than previous hotkey input, the user started a new Alt+` sequence, otherwise it's a continuation of the same sequence.

Related

GTK4 under rust, trying to find which mouse pointer and whether there are modifer keys pressed

I'm writing a gtk4 application in rust, and I'd like to get the mouse key that was clicked and any modifier keys at the time. My GestureClick handler has .buttons(0) to accept input from all buttons, but the event object does not seem to have any way to determine the source of the event. The event just gives the GestureClick object, location, and click count. Does it need to have a separate handler for each button?
In addition, I'd like to use modifier keys like shift and ctrl to modify the action. This does not seem to be available (at least, I haven't been able to find it). Would I need to monitor keypress events to keep track of the state myself? I'm pretty sure X reports the state of the modifier keys on click events, gtk doesn't?
Thanks

Using KeyRemap4MacBook to bind a keyboard key to a scroll action?

Basically, I want to bind a certain letter to scroll up the mouse reel, and do so continuously if I hold the key continuously. I need it to work even if other keys are being pressed at the same time, and only work on a certain window title/tab title in the browser.
I basically want to emulate the function of the autohotkey program for the "Attack On Titan Tribute Game"'s reeling function.
An AutoHotKey equivalent is available at:
http://fenglee.com/forum/viewtopic.php?f=2&t=7548
(Which may be offline due to unknown reasons, might have to wait)
Basically, if you download the application, set Reel In to the "x" or "e" key, it works regardless if other keys are being held. Just make sure you uncheck the checkbox so it can work in any window. It basically scrolls down for you. It also includes the source.
Or.. if I can use any other free application to do the same thing, that'd be nice.
All I want is the scrolling function and it has to be specific to a window/plugin/etc. and be able to be triggered while holding down other keys.

GetAsyncKeyState() causes anti-virus program to flag as keylogger

I have been building a very small game in the Windows API, and in the main message loop I use GetAsyncKeyState() to test if a user is pressing the arrow buttons. I use this instead of WM_KEYDOWN because with WM_KEYDOWN there is an initial pause after the first press, and I don't want to modify a user's settings. My antivirus program flags the game as a keylogger program, is there an alternative way about this?
How is the anti-virus program supposed to guess that you are not using GetAsyncKeyState() to spy on the keyboard and log keys? You tell it of course, make an exclusion. If you're worried that your future customers are not so easily convinced then go back to using WM_KEYDOWN/UP. Use an array of 256 bools to keep track of the key state. Set it to true on DOWN, regardless of how many you get, false on UP. Also check if the scanner is happy when you stop calling the API function when your app loses focus. Pay attention to WM_ACTIVATEAPP.

CGEventCreateKeyboardEvent on Desktop vs MacBook

Ola Folks,
Once again I want to drink from the pool of knowledge shared by people using SO.
I have written a small app for OSX that sends key events to an application. I am targeting OSX 10.5.x and newer. However, the problem exists when I build for 10.6.x as well. Everything works fine except when I send only the modifier keys; Alt, Command, Control and Shift.
The problem is that on the two MacBooks, the events for the modifier keys appear to be cleared as soon as the testers move the cursor using the mouse or touch the touchpad.
On a desktop with XCode installed, everything works fine. Just like it should. On two different MacBooks, the problem occurs. The desktop has a standard 101 key keyboard and a multi-button mouse attached.
When a mouse is connected to the MacBooks, a two button mouse with scrollwheel is used. However, the problem exists when no peripherals are attached and the touchpad is used.
What I expect to happen is that the Modifier Key event is sent to the target application, the user moves the cursor using the mouse / touchpad, press buttons on the mouse / touchpad and/or press keys on the keyboard with the modifier key down event 'active'. Then, when they finish, the key up event for the modifier key is sent.
Here is how I am sending key down events, the Shift key for this example:
case ModKeyShiftDown:
xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, true);
CGEventSetFlags(xTheCommand, kCGEventFlagMaskAlternate);
CGEventPost(kCGHIDEventTap, xTheCommand);
//CGEventPost(kCGSessionEventTap, xTheCommand);
//CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
CFRelease(xTheCommand);
CFRelease(xEventSource);
break;
I have used all three flags for creating the event source (kCGEventSourceStatePrivate, kCGEventSourceStateCombinedSessionState and kCGEventSourceStateHIDSystemState).
I have tried Creating the Keyboard Event with the Event Source as well as null as the first parameter.
I have tried with and without the appropriate flags on the event.
I have tried various combinations of posting the event; kCGHIDEventTap, kCGSessionEventTap and kCGAnnotatedSessionEventTap.
For completeness, here is how I send an up event for the Shift key:
case ModKeyShiftUp:
xEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
xTheCommand = CGEventCreateKeyboardEvent(xEventSource, kVK_Shift, false);
CGEventSetFlags(xTheCommand, 0);
CGEventPost(kCGHIDEventTap, xTheCommand);
//CGEventPost(kCGSessionEventTap, xTheCommand);
//CGEventPost(kCGAnnotatedSessionEventTap, xTheCommand);
CFRelease(xTheCommand);
CFRelease(xEventSource);
break;
When the testers trigger a modifier key down event, they can see the cursor change as expected. This lets me know the event is being processed by the target application. However, as soon as they touch the mouse or touchpad, the cursor changes back to a standard cursor and the mouse events are processed as if no modifier key events are active.
I would like to know if there is a problem with the way I am sending the events. I would also like to know if there is an alternate way to send Modifier Key events that is Going To Work.
Sorry if I overtalked this. My excuse is that I only slept a couple of hours. :P
Thanx
-isdi-
Ola,
Interestingly enough, using AXUIElementRef and AXUIElementPostKeyboardEvent seem to work.
For whatever reason, sending key events using the Accessibility object and method above solves the problem for the testers.

Setting Virtual Key/MouseButton State Without Triggering Events

Is it possible to set the virtual key state / mouse button state for all programs on a computer without triggering the associated events at the same time (like setting the left mouse button to be currently pressed without sending a mouse down event). Could this be achieved by setting up a fake keyboard or mouse driver for a fake keyboard or mouse that when queried as to the current state of a key would reply giving an answer of your choice or by any other means?
Edit:
This has to affect programs that I do not have the code for running in other threads ect...
Well, I don't have a complete answer for you but...
The Win32 function SetKeyboard State() changes the state of the keyboard for the thread that called it.
I know this does not affect the entire system, but if you only need the keyboard state changed for applications you are developing, then this could be called by each program, reading in the array passed to it from a temporary file on the harddrive.
Otherwise... don't know of anything else offhand...

Resources