Change Keyboard Layout with Delphi on Windows - 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}

Related

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

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;

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;

How do I add dock-icon badge and popupmenu support for lazarus apps in OSX?

I've tried googling around abit but I cannot find any help in using the badge feature of dock-icons on OSX aswell as getting access to the dock icon menu? I guess I could change the dock icon during run to indicate something is up but it isn't as sleek ;)
This feature isn't implemented in LCL, so if you want to use it, you will have to use the relevant Cocoa framework directly. You can use ObjPas for that. Of course if you are up for writing an LCL implementation, that would be a better long term solution, as it could be made to work on Windows/Gnome later.
Ludicrous late ... but I bumped into this post, and found this post in the Lazarus Forum, which shows code how you can change the application icon in the dock while the application is running.
Hope it will be of use for someone looking for an answer to the same question, even though it's years after the post of the original question. (apologies if this is not appropriate)
uses
... MacOSAll ...
procedure TFrm_Main.FormCreate(Sender: TObject);
begin
...
FResPath := TrimFilename(ExtractFilePath(Application.ExeName) + PathDelim + 'Resource');
...
end;
procedure TFrm_Main.SomeEventWhenOverlay(SomeVar: Integer);
var
temp_ImagePath: String;
temp_CGDataProvider: CGDataProviderRef;
temp_Float32Ptr: Float32Ptr;
temp_CGImage: CGImageRef;
temp_CGContext: CGContextRef;
begin
temp_ImagePath := TrimFilename(FResPath + PathDelim + 'Image' + PathDelim + 'overlay_image.png'); // image must be same size as icon, if not, will be deformed
if (FileExists(temp_ImagePath)) then
begin
temp_CGDataProvider := CGDataProviderCreateWithFilename(PChar(temp_ImagePath));
temp_Float32Ptr := nil;
temp_CGImage := CGImageCreateWithPNGDataProvider(temp_CGDataProvider, temp_Float32Ptr, 1, kCGRenderingIntentDefault);
CGDataProviderRelease(temp_CGDataProvider);
// Draw image
temp_CGContext := BeginCGContextForApplicationDockTile;
//SetApplicationDockTileImage(temp_CGImage);
OverlayApplicationDockTileImage(temp_CGImage);
CGImageRelease(temp_CGImage);
EndCGContextForApplicationDockTile(temp_CGContext);
end;
end;
procedure TFrm_Main.SomeOtherEventWhenRestore();
begin
//This will not work if you use SetApplicationDockTileImage
RestoreApplicationDockTileImage;
end;

Background changes by itself and procedure repeats many times until I release the mouse button

I am a student, and I'm working on a little slots game (if the same random number comes up 3 timed, you win). I use Borland Pascal 7. I use graph to make this a bit more visual, but when I start the game my background turns from black to grey, and the other problem is that if I click the game start button, the game runs many times until I release the mouse button. How can I solve this?
Here is my full program:
program slots;
uses mymouse,graph,crt;
var gdriver,gmode,coin:integer;
m:mouserec;
a,b,c,coins:string;
procedure gomb(x1,y1,x2,y2:integer;szoveg:string);
var j,n:integer;
begin
setcolor(blue);
rectangle(x1,y1,x2,y2);
setfillstyle(1,blue);
floodfill(x1+2,y1+2,blue);
setcolor(0);
outtextxy((x1+x2)div 2 -textwidth(szoveg) div 2 ,(y1+y2) div 2-textheight(szoveg) div 2,szoveg);
end;
procedure randomal(var a,b,c:string);
begin
randomize;
STR(random(2)+1,a);
STR(random(2)+1,b);
STR(random(2)+1,c);
end;
procedure menu;
begin;
settextstyle(0,0,1);
outtextxy(20,10,'Meno menu');
gomb(20,20,90,50,'Teglalap');
gomb(20,60,90,90,'Inditas');
gomb(20,100,90,130,'Harmadik');
gomb(20,140,90,170,'Negyedik');
end;
procedure teglalap(x1,x2,y1,y2,tinta:integer);
begin
setcolor(tinta);
rectangle(x1,x2,y1,y2);
end;
procedure jatek(var a,b,c:string;var coin:integer;coins:string);
begin;
clrscr;
menu;
randomal(a,b,c);
if ((a=b) AND (b=c)) then coin:=coin+1 else coin:=coin-1;
settextstyle(0,0,3);
setbkcolor(black);
outtextxy(200,20,a);
outtextxy(240,20,b);
outtextxy(280,20,c);
STR(coin,coins);
outtextxy(400,400,coins);
end;
procedure eger;
begin;
mouseinit;
mouseon;
menu;
repeat
getmouse(m);
if (m.left) and (m.x>20) ANd (m.x<90) and (m.y>20) and (m.y<50) then teglalap(90,90,300,300,blue);
if (m.left) and (m.x>20) AND (m.x<90) and (m.y>60) and (m.y<90) then jatek(a,b,c,coin,coins);
until ((m.left) and (m.x>20) ANd (m.x<140) and (m.y>140) and (m.y<170));
end;
begin
coin:=50;
gdriver:=detect;
initgraph(gdriver, gmode, '');
eger;
end.
I have many years to use Turbo Pascal :)
I used this snippet to init BGI (graphic) mode:
Gd := Detect;
InitGraph(Gd, Gm, 'bgi');
if GraphResult <> grOk then
Halt(1);
SetBkColor(black);
Cleardevice;
If I recall correctly, ClearDevice is proper for clearing the screen, ClrScr is for text mode.
Now, GetMouse(m); probably returns immediately the mouse data thus the code
in the repeat loop runs again and again with no delay, even if you don't use the mouse.
One solution is to check if the mouse button is up before you execute that code or
add some kind of delay before calling the GetMouse.

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