Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

How to use the GetKeyName API in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kurotsuke - 01 Jan 2005 22:08 GMT
Hi,

I'm trying to use the GetKeyName API to get the name of certain keys
(in the system language). For example the names of SHIFT and RETURN
keys as well as the names of same special characters.

I tried to call the API GetKeyName passing the KeyCode of the KeyUP
event but got no result

scancode = (uint) e.KeyCode;
StringBuilder sb= new StringBuilder(260);
int ret=GetKeyNameText(scancode,sb,20);

[DllImport("user32.dll")]
static extern int GetKeyNameText(uint lParam, [Out] StringBuilder
lpString, int nSize);

Sometimes the APi return 0 and sometimes it returns the name of
another character.

Can somebody post an example on how to use it? I could not find
anything worthwhile around.

Thanks.
Chris Taylor - 02 Jan 2005 01:52 GMT
Hi,

I never tested this, but try using e.KeyValue rather than e.KeyCode.

Hope this helps

Signature

Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

> Hi,
>
[quoted text clipped - 20 lines]
>
> Thanks.
Chris Taylor - 02 Jan 2005 02:40 GMT
Hi,

I took a quick look at the MSDN docs regarding this function and put
together the following code that should help you get started.

   [DllImport("user32", SetLastError=true, CharSet=CharSet.Unicode)]
   static extern int GetKeyNameTextW(uint lParam, StringBuilder lpString,
int nSize);

   [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
   static extern uint MapVirtualKeyW(uint uCode, uint uMapType);

   private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
   {
     StringBuilder sb= new StringBuilder(260);

     uint key = MapVirtualKeyW((uint)e.KeyCode, 0) << 16;
     int ret=GetKeyNameTextW(key, sb, 260);

     label2.Text = sb.ToString();
   }

You will notice that I had to use MapVirtualKey to map the key code and then
when passing the code to the GetKeyNameText function I performed a shift, in
the docs you will see that the scan code needs to be in bits 16..23
therefore I needed to shift the returned code to the correct position.

Hope this helps

Signature

Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

> Hi,
>
[quoted text clipped - 20 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.