Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET convert
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;
 

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 10-Apr-13 12:32pm
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
bEGI23 at 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 at 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
bEGI23 at 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 at 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
bEGI23 at 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
Sergey Alexandrovich Kryukov at 12-Apr-13 15:52pm
   
I see. Do you mean using of Windows Hook? First question I would ask: why getting output when a control in not in focus. The OS is build so that is should not happen. Just why? what application goals might possibly required that?
—SA
bEGI23 at 12-Apr-13 17:34pm
   
Just like i told you i need this, because my App needs it and the App will not be focused or at least not always, just to say like a log of the Keyboard. I then later will enhace the App so it can detect words and it will be always Top-Most.
Im trying to create a simple translator which will search the words and translate it automatically to the other language while the user is always focused on the Webbrowser(from 3rd Party) and he actually sees what the word translated means

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

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100