## Virtual Keyboard

Please discuss general Delphi programming topics here.

### Virtual Keyboard

Hello, i newer in delphi and i was wondering implement a keyboard, because i have a touchscreen.

I wanna do a keyboard almost like the " virtual keyboard " which comes with windows.

I started to use the method keybd_event to send the values of buttons to another application ... but all the time it loses de focus....

i wanna press the key " a " in my application and send this value to another application... simulating as i am typing in the real keyboard...

has anyone tried something like this? I went on google but i did not find how to implent a thing like that.

tanks for any information
Member

Posts: 3
Joined: September 10th, 2008, 2:14 pm
Location: Florianopolis - Brazil

Here is the trick:

Code: Select all
procedure TYourControl.CreateParams(var Params: TCreateParams);const  WS_EX_NOACTIVATE = $08000000;begin inherited CreateParams(Params); // At least Windows 2000/XP if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion >= 5) then Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE;end;procedure TYourControl.WMMouseActivate(var Message: TWMMouseActivate);begin Message.Result := MA_NOACTIVATEend; By the way, it's better to use SendInput instead of keybd_event. Kambiz Kambiz Administrator Posts: 2429 Joined: March 7th, 2003, 7:10 pm Hello man, your help was pretty good. I tried using the following code and it worked almost awesome. The problem is that it always overwrite the last letter, so i put the keybd_event(37, 0, 0, 0); but with this it puts the cursor to back. But it works not in a properly way, but it is ... So do you know how i could fix it and make the cursor work properly as in the " windows virtual keyboard " ??? By the way the code bellow is fully functional and u can test there. thanks for any information. Code: Select all  unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm2 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public MyHandle : THandle; //Ax : THandle; HandleMaeFocus : THandle; HandleFilhaFocus : THandle; end;var Form2: TForm2;implementation{$R *.dfm}//============================================================================//procedure TForm2.Button1Click(Sender: TObject);//============================================================================//begin  Application.HandleMessage;  //Handle da janela do teclado  MyHandle := FindWindow('TForm2',nil);    if GetActiveWindow = MyHandle then    HandleMaeFocus := GetNextWindow(MyHandle,GW_HWNDNEXT);    Sendmessage(HandleFilhaFocus,WM_SETFOCUS,0,0);  SetActiveWindow(HandleFilhaFocus);  //Caso esteja digitando no browser HERE ---------<<<<<<<<<---////  keybd_event(37, 0, 0, 0);    //Código ascii da letter A  keybd_event(65, 0, 0, 0);end;end.
Member

Posts: 3
Joined: September 10th, 2008, 2:14 pm
Location: Florianopolis - Brazil

Once I had to write a commercial Virtual Keyboard component with dynamic layout design, word prediction, abbreviation support, and some other fancy (or maybe stupid) features. First it was sounding simple but took one full month to get functional.

Here is a list of things that I remember and you have to consider while developing your virtual keyboard:
• You should reset toggling keys (e.g. shift, ctrl, alt, ...) before sending a key, and the restore them back.
• Not only you have to send a pressed key, but also you have to release it.
• For character keys should to use WM_CHAR message and only its unicode version on NT, XP, and Vista. For other keys you should use SendInput.
• GetFocus and GetActiveWindow return valid values for current process only. You should either attach to other processes' input queue or call GetGUIThreadInfo.
• Don't forget to manage dead keys, which is the most difficult part in my opinion. Do not know what is a dead key? Google it!
• Before sending and key/character to another process, you should wait for the process to be ready.
• Use scan codes to determine actual VKey or Character pressed.
• Notice that actual character of a scan code depends on the selected keyboard layout (language) selected by the user.
Good luck!
Kambiz

Kambiz

Posts: 2429
Joined: March 7th, 2003, 7:10 pm

ohh man :O ... im kinda worried, i thought it would be simple ... i started to learn delphi around 2 months ago and im liking it , but somethings like that make me sad... but i like the challenge to learn new things whatever.... thanks for your advices, i will try to implement the keyboard and if some question come up i gonna post here hehe

well thanks again for your help again.