I used to use the RegisterHotkey method but users have had issues based on their keyboard. For example, the hotkey does not work while a command prompt is focused. Or if the ctrl+alt+del dialog is focused. The LowLevelKeyboardHook worked uniformly across all users.
I had an issue though with modifiers. For instance if the hotkey is Win+F. If the user first:
downs the Win, I allow it
then downs the F key, I block it
if user ups the F key then I block it too
however, on up of the Win key, it comes through and the start menu opens
I tried blocking the Win key on step 4. However this causes it stays depressed in the keyboard buffer (pressing L for instance will lock the computer).
Is there a way to clear the keyboard buffer?
I was thinking of blocking the up, but after all keys are released, then clear the buffer.
I tried the method from this SO answer, but it didn't work:
#include <conio.h>
while (_kbhit()) {
_getche();
}
Related
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.
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.
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.
How can I fire an automatic key press or mouse click event when a color appears on the screen
on other application or browser?
It depends a lot on what you want. Do you want to send the keys to
your Application
another fixed Application
Simulate a global keypress
Simulating keys globally
All of these will cause problems targeting a specific application and the active window changes.
SendKeys Sends Messages to the active app. It's a high level function taking a string which encodes a sequence of keys.
keybd_event is very low level and injects a global keypress. In most cases SendKeys is easier to use.
mouse_event simulates mouse input.
SendInput supersedes these functions. It's more flexible but a bit harder to use.
Sending to a specific window
When working with a fixed target window, sending it messages can work depending on how the window works. But since this doesn't update all states it might not always work. But you don't have a race condition with changing window focus, which is worth a lot.
WM_CHAR sends a character in the basic multilingual plane (16 bit)
WM_UNICHAR sends a character supporting the whole unicode range
WM_KEYDOWN and WM_KEYUP Sends keys which will be translated to characters by the keyboard layout.
My recommendation is when targeting a specific window/application try using messages first, and only if that fails try one of the lower level solutions.
when a color appears on the screen on other application or browser
I made one program using OpenCV and C++ for operating mouse with finger gesture. I used 3 color strips for 3 mouse function.
Yellow color for Left click
Blue color for Right click
Pink color for controlling cursor position
Whenever camera detect these colors, associated function takes place, I have used mouse_event for performing mouse function.
For more information you may read my code, blog, video.
I'm not 100% sure what you want, but if all you are after is running the method linked the the button.Clicked event, then you can manually run the method just like any other method.
You can use the .NET SendKeys class to send keystrokes.
Emulating mouse clicks requires P/Invoke.
I don't know how to detect colors on the screen.
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...