473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

GetKeyboardState Problem in C#

I have the code below to convert Virtual Keys using the Keyboard
state. The problem is that it seems that GetKeyboardState is not
working properly. The state comes back the same each time. For
example, I start the app with CAPS off and press the "f" key. ToAscii
returns the result of "f" properly. Then I press the CAPS ON and I
still get "f" from ToAscii. Some debugs reveal that he keyboard state
had not changed after the CAPS where put on.

If I restart the APP with CAPS ON I get "F" properly but then when I
turn CAPS off I still get "F".

It seems GetKeyboardState is returning the state the keyboard was in
when the app started.

Any Ideas?

[DllImport("user32.dll")]
public static extern bool ToAscii(int VirtualKey, int ScanCode,
byte[] lpKeyState, ref uint lpChar, int uFlags);

// Allows us to get current keyboard state.
[DllImport("user32.dll")]
public static extern int GetKeyboardState(byte [] lpKeyState);

protected char GetAsciiCharacter(int iKeyCode, int iFlags)
{
uint lpChar = 0;

byte[] bCharData = new byte[256];

GetKeyboardState(bCharData);

for(int i=0; i < 256; i++)
{
Console.WriteLine("[{0}] {1} {2}",i,
lastbCharData[i],bCharData[i]);
}
ToAscii(iKeyCode, iFlags, bCharData, ref lpChar, 0);
for(int i=0; i < 256; i++)
{
lastbCharData[i] =bCharData[i];
}
return (char)lpChar;
}

this is how the method is called:
Console.WriteLine("Key Pressed GetAscii: " +
GetAsciiCharacter(hookStruct.vkCode, hookStruct.scanCode));
Nov 15 '05 #1
1 11948
Hi sharp,
I tried your code and it works fine, but in different context.
It looks like that code snippet you posted is taken off of some windows
hook.
So I didn' try it with a windows hook. I just call the GetAsciiCharacter
method from form's keydown event

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
Console.WriteLine("Key Pressed GetAscii: " +
GetAsciiCharacter((int)e.KeyCode, e.KeyValue));
}

In this context your method returns correct results.

What might be the problem? If you read in the MSDN about GetKeyboardState
you can find the following:

"An application can call this function to retrieve the current status of all
the virtual keys. The status changes as a thread removes keyboard messages
from its message queue. The status does not change as keyboard messages are
posted to the thread's message queue, nor does it change as keyboard
messages are posted to or retrieved from message queues of other threads.
(Exception: Threads that are connected through AttachThreadInput share the
same keyboard state.)"

So it might be possible that you have thread issues.

--
HTH
B\rgds
100

"sharp" <sh***@homerlex.mailshell.com> wrote in message
news:fd*************************@posting.google.co m...
I have the code below to convert Virtual Keys using the Keyboard
state. The problem is that it seems that GetKeyboardState is not
working properly. The state comes back the same each time. For
example, I start the app with CAPS off and press the "f" key. ToAscii
returns the result of "f" properly. Then I press the CAPS ON and I
still get "f" from ToAscii. Some debugs reveal that he keyboard state
had not changed after the CAPS where put on.

If I restart the APP with CAPS ON I get "F" properly but then when I
turn CAPS off I still get "F".

It seems GetKeyboardState is returning the state the keyboard was in
when the app started.

Any Ideas?

[DllImport("user32.dll")]
public static extern bool ToAscii(int VirtualKey, int ScanCode,
byte[] lpKeyState, ref uint lpChar, int uFlags);

// Allows us to get current keyboard state.
[DllImport("user32.dll")]
public static extern int GetKeyboardState(byte [] lpKeyState);

protected char GetAsciiCharacter(int iKeyCode, int iFlags)
{
uint lpChar = 0;

byte[] bCharData = new byte[256];

GetKeyboardState(bCharData);

for(int i=0; i < 256; i++)
{
Console.WriteLine("[{0}] {1} {2}",i,
lastbCharData[i],bCharData[i]);
}
ToAscii(iKeyCode, iFlags, bCharData, ref lpChar, 0);
for(int i=0; i < 256; i++)
{
lastbCharData[i] =bCharData[i];
}
return (char)lpChar;
}

this is how the method is called:
Console.WriteLine("Key Pressed GetAscii: " +
GetAsciiCharacter(hookStruct.vkCode, hookStruct.scanCode));

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ed | last post by:
I've been away from C++ programming for awhile. Maybe this is a simple problem. I get the error message "illegal call of non-static member function" when I insert DWORD hotkey;...
5
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
10
by: lgbjr | last post by:
Hi All, In VB.Net how do I determine if CapsLock is on/off (same for NumLock) and whether the input mode is set to Insert or Overwrite (Insert Key). I just added a status bar to my app, and I...
5
by: Todd Snyder | last post by:
I need to make a field put uppercase letters in it when a lowercase letter is entered into that field. Is there an expression to use or does it have to be written in code? *** Sent via...
6
by: Steve Barnett | last post by:
Is there a .Net equivalent of the GetKeyboardState api? I need to know whether the user is pressing the Ctrl or Shift keys when they click on my usercontrol. Thanks Steve
6
by: Julian | last post by:
I would like my Post Code field to be autoformated in CAPS when entered in the table. How do I do that? Julian
3
by: Me | last post by:
Trying to get the current status of the insert key. Found my.computer.keyboard.capslock, but no such item for insert key. Anyone have some code to help. Can't figure out getkeystate. compiler...
17
by: martin DH | last post by:
Hello, I would like to have all data entered into every form in my database in all capital letters as a default. Is there a way to go about this? martin DH
5
by: Matuag | last post by:
How can I enable Caps Lock after the form or database opens?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.