473,770 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetKeyboardStat e Problem in C#

I have the code below to convert Virtual Keys using the Keyboard
state. The problem is that it seems that GetKeyboardStat e 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 GetKeyboardStat e is returning the state the keyboard was in
when the app started.

Any Ideas?

[DllImport("user 32.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("user 32.dll")]
public static extern int GetKeyboardStat e(byte [] lpKeyState);

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

byte[] bCharData = new byte[256];

GetKeyboardStat e(bCharData);

for(int i=0; i < 256; i++)
{
Console.WriteLi ne("[{0}] {1} {2}",i,
lastbCharData[i],bCharData[i]);
}
ToAscii(iKeyCod e, 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.WriteLi ne("Key Pressed GetAscii: " +
GetAsciiCharact er(hookStruct.v kCode, hookStruct.scan Code));
Nov 15 '05 #1
1 11991
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 GetAsciiCharact er
method from form's keydown event

private void Form1_KeyDown(o bject sender, System.Windows. Forms.KeyEventA rgs
e)
{
Console.WriteLi ne("Key Pressed GetAscii: " +
GetAsciiCharact er((int)e.KeyCo de, e.KeyValue));
}

In this context your method returns correct results.

What might be the problem? If you read in the MSDN about GetKeyboardStat e
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 AttachThreadInp ut 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.goog le.com...
I have the code below to convert Virtual Keys using the Keyboard
state. The problem is that it seems that GetKeyboardStat e 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 GetKeyboardStat e is returning the state the keyboard was in
when the app started.

Any Ideas?

[DllImport("user 32.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("user 32.dll")]
public static extern int GetKeyboardStat e(byte [] lpKeyState);

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

byte[] bCharData = new byte[256];

GetKeyboardStat e(bCharData);

for(int i=0; i < 256; i++)
{
Console.WriteLi ne("[{0}] {1} {2}",i,
lastbCharData[i],bCharData[i]);
}
ToAscii(iKeyCod e, 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.WriteLi ne("Key Pressed GetAscii: " +
GetAsciiCharact er(hookStruct.v kCode, hookStruct.scan Code));

Nov 15 '05 #2

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

Similar topics

6
6720
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; hotkey=CHotKeyCtrl::GetHotKey(); into a Wizard-generated function that is NOT static -- void CCubesView::OnLButtonUp(UINT nFlags, CPoint point) I am using MFC provided by the Wizard. I #include "stdafx.h" (it's actually in afxcmn.h).
5
3446
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
10
7801
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 want to reflect the state of these three items (similar to Word, Excel, etc.) I've found the key enumerator value for each key, but I don't know how to tell if the state is on/off, etc. TIA Lee
5
3608
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 Developersdex http://www.developersdex.com ***
6
17856
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
1737
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
5833
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 chokes on rslt = getkeystate(keys.insert) ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
17
11547
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
3984
by: Matuag | last post by:
How can I enable Caps Lock after the form or database opens?
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.