Windows api: wait until all keyboard keys are released system wide - windows

I have made an application in Delphi that handles some defined system wide hotkeys, works perfectly. However, for some hotkey functionality I have to trigger/simulate some keyboard strokes such as ALT+ENTER. This works great when the user releases the hotkey keys directly, but when the keys are still pressed by the user the keyboard simulation fail.
Is there a way (with windows API) to check if all keys are released before I process a keyboard simulation?

Use GetAsyncKeyState as this API reflects the true current state of the keyboard, and not when your app last called GetMessage. Just write a loop that calls it for each value between 0 and 0xFF.
If the most significant bit is set, the key is down

Thanks go to #David Ching and #David Heffernan (two Davids!) The solution is not only to test keyboard input but also mouse input or better, the state of input devices.
The mouse is also included because of:
{ Virtual Keys, Standard Set }
VK_LBUTTON = 1;
VK_RBUTTON = 2;
VK_MBUTTON = 4; { NOT contiguous with L & RBUTTON }
So, if don't want to test mousebuttons you have to exclude it from the loop. It's better to check these also because there are hotkeys that must be used with the mouse. It's better to check everything on input is idle.
function isUserInputDevicesInUse() : Boolean; // Keyboard pressed / mouse pressed?
var
i : LongInt;
begin
i:=256;
Result:=FALSE;
while( i > 0 ) and ( NOT Result ) do
begin
Dec( i );
Result:=( GetAsyncKeyState(i) < 0 );
end;
end;
function isUserInputDevicesIdle() : Boolean;
begin
Result:=NOT isUserInputDevicesInUse();
end;

Related

How to get focused child elements of windows pane control?

I use windows UI Automation framework to access controls in other processes. I catch the system SETFOCUS messages and check the type of the focused control if it is an edit control or not. This sometimes works perfectly, but sometimes I won't get the focused control from setfocus message but only a handle to an upper control in the tree, for example a handle to a pane. What am I doing wrong?
I tried to find out which child element of the pane currently got keyboard focus by checking the UIA_HasKeyboardFocusPropertyId of the enabled child elements, but all of them will return false.
Below is the code which checks the keyboard focus property.
Additionally, for some reason I am not able to use the Automation Element Property Identifiers. They should be listed here: https://msdn.microsoft.com/en-us/library/windows/desktop/ee684017(v=vs.85).aspx
I got the values from archive.org, because the content is no longer available.
In the code, "i" is the type of the currenty focused control which got the windows setfocus message.
if (i = 50033) then // 50033 (pane)
begin
uiAuto.CreatePropertyCondition(30010, true, cond); // 30010 (enabled property)
if cond <> nil then
begin
focusedElement.FindAll(TreeScope_Children, cond, children);
if children <> nil then
begin
children.Get_Length(length);
if length > 0 then
begin
Memo1.Lines.Add('length: ' + IntToStr(length));
for j := 0 to length-1 do
begin
children.GetElement(j, tempChildElement);
tempChildElement.Get_CurrentControlType(k);
Memo1.Lines.Add('child element type: ' + IntToStr(k));
tempChildElement.GetCurrentPropertyValue(30008, keybFocusBool); // 30008 (UIA_HasKeyboardFocusPropertyId)
if keybFocusBool then
Memo1.Lines.Add('child has keyboard focus: TRUE');
keybFocusbool := false;
end;
end;
end;
end;
end;

Change Keyboard Layout with Delphi on Windows

I want to change the keyboard layout in Windows with Delphi. I successfully could do this with following code but it seems it only changes for
one process/thread and not for every process.
// Array with 4 keyboard layout codes (in decimal)
const KLS: array [0 .. 3] of integer = (2055, 4108, 2064, 2057);
KLF_SETFORPROCESS = $00000100;
// The TForm1 contains a RadioGroup1 with 4 Radiobuttons
procedure TForm1.btn_activateLayoutClick(Sender: TObject);
begin
SetKeyboardLayout(RadioGroup1.ItemIndex);
end;
// set the new keyboard layout according to the ItemIndex of the RadioGroup1
procedure TForm1.SetKeyboardLayout(const klsIndex: integer);
var
klId: array [0 .. 9] of char;
keyboardCode: integer;
begin
keyboardCode := KLS[klsIndex];
try
ActivateKeyboardLayout(keyboardCode, KLF_SETFORPROCESS or KLF_ACTIVATE, KLF_SETFORPROCESS)
finally
raise Exception.Create('Error while changing keyboard layout');
end;
end;
end.
Does anyone know how I could change the keyboard layout for every process in Windows? Do I need to make some changes in Registry?
Based on your comments it seems that what you want is to prevent the RDP connection from changing your local keyboard layout settins.
Answer to this can be found here:
https://superuser.com/questions/426356/how-can-i-stop-the-remote-computer-from-changing-my-keyboard-layout
//Winapi.windows
LoadKeyBoardLayout('0000040A',1); //Spanish keyboard
LoadKeyBoardLayout('00000409',1); //English (US) keyboard
//https://learn.microsoft.com/en-us/windows/iot-core/develop-your-
app/onscreenkeyboardlayouts windows page for keyboards
{The keyboard has to be loaded already under "Region & Language" -->
English (United States) --> Options --> +Add a keyboard --> US QWERTY
and Spanish QWERTY}

Fix multiple middle mouse click

Basically I have an issue where the middle mouse button when clicked does multiple very fast middle mouses. For example, if I open a link in a new tab with middle mouse it will open about 10 of that tab. I have tried all of the conventional methods to fix it, ie. driver fixes etc. What I want to try now is a bit of mouse debouncing with AHK (Auto Hot Key) for windows.
Essentially what I am thinking is to do this:
while (forever)
if( capture the middle mouse)
sleep 500 ms
mouse click
end
end
Can anyone give some advice with this approach?
Alternatively i thought about making a middle mouse hotkey:
$MButton::
Loop
{
sleep 500
if not GetKeyState("MButton", "P")
break ; Break out of the loop.
}
send {MButton}
return
Can anyone see any problems with this?
You can have a much simpler solution without a delay.
This will ignore middle click if the last click was 50 ms ago.
#Persistent
global pressed_g := 0
global delay_g := 50 ; delay in miliseconds, increase this value if your multiple click take longer than delay_g time
return
MButton::
if( pressed_g = 0 )
{
Send, {MButton}
tooltip,sent
pressed_g := 1
}
SetTimer, Countdown , Off
SetTimer, Countdown , -%delay_g%
return
Countdown:
pressed_g := 0
return
Could it be that you are looking for this? You press the MButton and while you keep the MButton pressed, the script will continue to fire MButton.
#Persistent
MButton::
while GetKeyState("MButton", "P") ; While the Middle Mouse button key is being held down
{
Send, {MButton}
}
return

Distinguish between left and right shift keys using RAWINPUT

RAWINPUT provides two flags (RI_KEY_E0 and RI_KEY_E1) to check whether the left or right version of a key is pressed. This works great for CTRL, but not for left and right shift. In fact, the flags are the same for both, and the VKey is also the same (VK_SHIFT). How can I find out which shift was pressed? I'm working on Windows 7. Interestingly, the flags/vkey values are exactly the same no matter which shift key I'm pressing.
Windows 7, and I only get VK_SHIFT, never the L/R variants
Which is part of the explanation why this doesn't work the way you think it should do. There's ancient history behind this. The keyboard controller was redesigned for the IBM AT, again for the Enhanced keyboard. It started sending out 0xe0 and 0xe1 prefixes for keys that were added to the keyboard layout. Like the right Ctrl and Alt keys.
But keyboards always had two shift keys. The original IBM PC didn't consider them special keys, they simply have a different scan code. Which was maintained in later updates. Accordingly, you don't get the RI_KEY_E0 or E1 flags for them. You have to distinguish them by the RAWKEYBOARD.MakeCode value. The left shift key has makecode 0x2a, the right key is 0x36.
Note that the left Ctrl and Alt keys don't have the flags either. They match the corresponding keys on the old PC keyboard layout. The description of the flags in the MSDN Library article is not very accurate.
You can distinguish left-right SHIFT/CONTROL/ALT VK codes like this:
case WM_INPUT:
{
HRAWINPUT dataHandle = reinterpret_cast<HRAWINPUT>(lParam);
RAWINPUT input;
UINT size = sizeof(input);
::GetRawInputData(dataHandle, RID_INPUT, &input, &size, sizeof(RAWINPUTHEADER));
if (input.header.dwType != RIM_TYPEKEYBOARD)
break;
const RAWKEYBOARD& keyboard = input.data.keyboard;
// Ignore key overrun state
if (keyboard.MakeCode == KEYBOARD_OVERRUN_MAKE_CODE)
return;
// Ignore keys not mapped to any VK code
// This effectively filters out scan code pre/postfix for some keys like PrintScreen.
if (keyboard.VKey >= 0xff/*VK__none_*/)
return;
uint16_t scanCode = keyboard.MakeCode;
// Scan codes could contain 0xe0 or 0xe1 one-byte prefix.
// See https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
scanCode |= (keyboard.Flags & RI_KEY_E0) ? 0xe000 : 0;
scanCode |= (keyboard.Flags & RI_KEY_E1) ? 0xe100 : 0;
uint16_t vkCode = keyboard.VKey;
switch (vkCode)
{
case VK_SHIFT: // -> VK_LSHIFT or VK_RSHIFT
case VK_CONTROL: // -> VK_LCONTROL or VK_RCONTROL
case VK_MENU: // -> VK_LMENU or VK_RMENU
vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX));
break;
}
//...
return 0;
}
This code should work at least from Vista.
But please note that gamedev programmers are usually manually mapping scancodes to internal game engine specific keycodes - because VK codes are tend to change on different keyboard layouts. For example if you use usual VK_W/VK_A/VK_S/VK_D for movement in QWERTY layout - it could turn into VK_Z/VK_Q/VK_S/VK_D in AZERTY keyboard layout. VK codes are primarily handy in Win32 GUI programming.
You can grab decent scancode<->USB HID Usage conversion table here: https://source.chromium.org/chromium/chromium/src/+/main:ui/events/keycodes/dom/dom_code_data.inc

How to get/set the scrollbar location of the browser (IE/Firefox/...)?

Is there a way to get/set the location of the scrollbar in Internet Explorer/Firefox?
I am not looking to do that from inside the HTML/ASP/Javascript code, but from an application outside the browser (using WinAPI for example), and without using BHO.
From the search I've done right now it seems impossible, so I'm dropping a question here as a last try.
For Internet Explorer, you can use COM automation to enumerate all active Internet Explorer windows/tabs and then access the DOM tree of the document displayed in the window/tab to access and read the scroll position.
The following sample code uses Delphi as a programming language. The mechanism would be similar in C++, VB or C#
var
ShWindows: ShellWindows;
InetExplorer: InternetExplorer;
Count: Integer;
I: Integer;
HTMLDocument: IHTMLDocument2;
Elem: IHTMLElement2;
ScrollPosY: Integer;
begin
// Create ShellWindows Object
SHWindows:= CoShellWindows.Create;
// Number of explorer windows/tabs (win explorer and ie)
Count:= ShWindows.Count;
ShowMessage(Format('There are %d explorer windows open.', [Count]));
// For all windows/tabs
for I:= 0 to (Count - 1) do
begin
// Get as InetExplorer interface
InetExplorer:= SHWindows.item(I) as InternetExplorer;
// Check to see if this explorer window contains a web document
if Supports(InetExplorer.Document, IHTMLDocument2, HTMLDocument) then
begin
// Get body Element
Elem:= HTMLDocument.body as IHTMLElement2;
// Read vertical scroll position
ScrollPosY:= Elem.scrollTop;
// If this is 0 so far, maybe there is a scroll position in root element
if ScrollPosY = 0 then
begin
Elem:= HTMLDocument.body.parentElement as IHTMLElement2;
ScrollPosY:= Elem.scrollTop;
end;
// Display
ShowMessage(IntToStr(Elem.scrollTop));
end;
end;
end;
For documentation, start here: http://msdn.microsoft.com/en-us/library/bb773974(VS.85).aspx

Resources