Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi people im trying to create an UI Application on which is shown what the User actually types and which will be shown on the Window of the Application, to say like a Bar on the top of the Desktop, on which the typed Keystrokes are presented and which will continuosly move from right to the left side, like in a Exchange you see.

My problem is i use the GetAsyncKeyState() API Function and this only represents the Ascci Keycodes and not the same as when you type in Textbox like "Here".
What i want to say is that it doesnt really recognize what the Keyboard commands(Shift & a, which returns 'a' but not 'A') and also it doesnt represent the Keyboard Languages like Arabic and Cyrillic or Chinese.

Is it even possible to automate an App to do this which less code?

My Example;


VB
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vkey As Integer) As Integer

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim Result as integer
For i = 1 to 255                                  
       Result = GetAsyncKeyState(i)
       If Result = -32767 then               
             textbox1.text = Cstr(chr(i))
       End If
Next
End Sub


Can you give me a tip where to search for the solution or is it something easy which i didnt yet realise?
Posted
Comments
Sergey Alexandrovich Kryukov 10-Apr-13 17:51pm    
The answer would be pretty simple (compare documentation for native Windows API and .NET virtual key codes), but... why would you do it, exactly?
You should also know that, 1) scan code is not ASCII, 2) virtual keys used in GetAsyncKeyState are not scan codes, 3).NET does not use ASCII (but supports the encoding).
—SA

1 solution

Please see more thoroughly: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx[^].

This API works with virtual keys, same as, for example, .NET System.Windows.Forms.KeyCode. Those key codes are not scan codes, and not character codes as ASCII or Unicode UTFs.

Basically, the keyboard is processed like this: scan code -> virtual keys -> characters (including delete) or non-character events (such as KeyUp, KeyDown for non-character keys).

Please also look at .NET keyboard events (no matter, System.Windows.Forms or WPF) and compare the data passed to event handlers with Windows API information. It should help you to understand what you are really doing. I, for example, don't understand what you are trying to achieve. Chances are, pure .NET FCL could help you more. Remember that P/Invoke can kill platform compatibility, so pure .NET is always better. I admit there are certain cases when using raw Windows API is unavoidable, but such cases are relatively rare. If you explain your goal, you can get a chance to receive some more advice.

—SA
 
Share this answer
 
Comments
[no name] 11-Apr-13 17:31pm    
So the Keyboard only sends the ScanCodes no matter what Keyboard it is, but the APIs do the hard job to convert these ScanCodes into their appropriate "Virtual Keyboard Language" while they compare it with the Language(for example US) and returns the appropriate Key.
Am i right?
Sergey Alexandrovich Kryukov 11-Apr-13 18:01pm    
Not AP, but the OS. You see, input language is switched by OS for each application individually, and then, some applications support Unicode, but some do not. And the application is agnostic on what is the input languages, which is natural; it does not depend on current thread culture and UI culture. OS simply takes "input mode" into account send whatever the user tries to input. But events like Alt+Key, or even Alt+Shift do not imply any characters at all; they are "input" by handling events like KeyDown, and the data on the "virtual key" is used. It is already abstracted from scan codes. Scan codes are something like a key number...
—SA
[no name] 12-Apr-13 4:31am    
Okey, so how do i get the Scan Codes and how to compare it manually with the Input Language? Does somewhere exist an example in Web no matter in what programming language but just to see how it is done. Is the "Input Language" something like a library file or what exactly and where is it located at?

Yes i know the Control Keys like Shift and Ctrl ... i have to manually compare with the ScanCode to return Uppercase and others.
Sergey Alexandrovich Kryukov 12-Apr-13 11:19am    
I used to read scan codes directly from the keyboard; this is the only way I knew. On Windows, it won't work, unless you write a kernel-mode low-level driver. You get get a scan code from virtual key code and back:
http://msdn.microsoft.com/en-us/library/ms646306%28v=VS.85%29.aspx

This is not what you want. You should really work with virtual key codes. Also, you don't need to get characters based on input language; you already get characters from OS. OK, if you explain me why, what you are trying to achieve, perhaps I'll be able to advise...

—SA
[no name] 12-Apr-13 15:02pm    
When i create a simple Windows Form with a Textbox in it and the Window which owns the Textbox has focus and also the Textbox Control has focus the Keystrokes i type from the Keyboard are stored(typed) in correct format, even when i change the Input Languages it stores the corresponding key, for example AL returns albanian keystrokes, DE returns german keystrokes, AR returns arabic keystrokes.
But when the Window has no focus then it wont recognize the Keystrokes anymore, thats normal and logical and i dont need chronic Focus, i dont need it at all.
So i thought i use the API - GetAsyncKey Function to globally catch the Keystrokes and send it as if i would directly write to the Textbox, but it doesnt, except ASCCI Keys. I have to manually hardcode them while comparing with Control-Keys and input languages. I also have an example of 'Hook' -ing Keyboard from a Keylogger Developer but yet it doesnt fit the needs, it seems only to catch Keycodes which represent an ASCCI key.
I think you know now what im trying to achieve.
Thanks in advance, Begi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900