473,776 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a character name

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(2 60);
int ret=GetKeyNameT ext(scancode,sb ,20);
[DllImport("user 32.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.
Nov 16 '05 #1
6 6936
Hi,

Please do not cross post, I answered this post on the interop group.

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("user 32", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern int GetKeyNameTextW (uint lParam, StringBuilder lpString,
int nSize);

[DllImport("user 32.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern uint MapVirtualKeyW( uint uCode, uint uMapType);

private void Form1_KeyDown(o bject sender,
System.Windows. Forms.KeyEventA rgs e)
{
StringBuilder sb= new StringBuilder(2 60);

uint key = MapVirtualKeyW( (uint)e.KeyCode , 0) << 16;
int ret=GetKeyNameT extW(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

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"kurotsuke" <ku*******@yaho o.it> wrote in message
news:0r******** *************** *********@4ax.c om...
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(2 60);
int ret=GetKeyNameT ext(scancode,sb ,20);
[DllImport("user 32.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
http://dotnetjunkies.com/weblog/chris.taylor
"kurotsuke" <ku*******@yaho o.it> wrote in message
news:f6******** *************** *********@4ax.c om... 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(2 60);
int ret=GetKeyNameT ext(scancode,sb ,20);
[DllImport("user 32.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.

Nov 16 '05 #2
Note that converting e.KeyCode into an int will give its .NET value in the
Keys enumeration which may or may not be equal to what the windows API
expects (I don't know). But here's a purely .NET way of doing it using
reflection, try it out and see if it gives you what you're looking for:

//in the KeyDown event handler:
string name = Enum.Parse(type of(Keys),
Convert.ToStrin g((int)e.KeyDat a)).ToString();
-Amit
"kurotsuke" <ku*******@yaho o.it> wrote in message
news:f6******** *************** *********@4ax.c om...
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(2 60);
int ret=GetKeyNameT ext(scancode,sb ,20);
[DllImport("user 32.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.

Nov 16 '05 #3
I just realized - it doesn't really use Reflection

"Amit" <re***********@ hotmail.com> wrote in message
news:uK******** ******@TK2MSFTN GP09.phx.gbl...
Note that converting e.KeyCode into an int will give its .NET value in the
Keys enumeration which may or may not be equal to what the windows API
expects (I don't know). But here's a purely .NET way of doing it using
reflection, try it out and see if it gives you what you're looking for:

//in the KeyDown event handler:
string name = Enum.Parse(type of(Keys),
Convert.ToStrin g((int)e.KeyDat a)).ToString();
-Amit
"kurotsuke" <ku*******@yaho o.it> wrote in message
news:f6******** *************** *********@4ax.c om...
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(2 60);
int ret=GetKeyNameT ext(scancode,sb ,20);
[DllImport("user 32.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.


Nov 16 '05 #4
But here's a purely .NET way of doing it


That gives the English names used in the enum, not the localized names
returned by GetKeyNameText.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
On Sun, 2 Jan 2005 04:56:06 +0200, "Chris Taylor"
<ch************ *@hotmail.com> wrote:
Hi,

Please do not cross post, I answered this post on the interop group.

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("user 32", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern int GetKeyNameTextW (uint lParam, StringBuilder lpString,
int nSize);

[DllImport("user 32.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern uint MapVirtualKeyW( uint uCode, uint uMapType);

private void Form1_KeyDown(o bject sender,
System.Windows .Forms.KeyEvent Args e)
{
StringBuilder sb= new StringBuilder(2 60);

uint key = MapVirtualKeyW( (uint)e.KeyCode , 0) << 16;
int ret=GetKeyNameT extW(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

--


Sorry for crossposting. After posting here I realized that maybe the
interop newsgroup was better.
I tried your solution and it worked. Will it work on Win98, too?

I'm having a minor problem. When a key on the numpad is pressed, the
description shown is always the numpad one (so I get 9 (tn) and never
get the pageup, pagedown, left, right... codes).
Have you got any idea on how to fix this? The Keycodes are different
for pageUp and (numpad 9)
Thanks again.
Nov 16 '05 #6
Hi,

To get the information for the extended keys I think your best bet is to go
slightly lower level and handle the windows keyboard messages as they are in
the windows message queue. Add the following code to a form and you will get
what you are looking for.

[DllImport("user 32", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern int GetKeyNameTextW (uint lParam, StringBuilder lpString,
int nSize);

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x100) // WM_KEYDOWN
{
StringBuilder sb= new StringBuilder(2 60);

int ret=GetKeyNameT extW((uint)m.LP aram.ToInt64(), sb, 260);
label2.Text = sb.ToString();
}

base.WndProc (ref m);
}

With regards to Win98, to use the GetKeyNameTextW you need the Microsoft
Layer for Unicode on Windows 95/98/Me Systems you can get this from the
following link
http://tinyurl.com/6wf9s
The original link is
http://www.microsoft.com/downloads/d...displaylang=en

I *think* for 98 you can import GetKeyNameText, but this does not exist on
Windows 2003 so your code would have to check what version of the OS it is
executing on and call the relevant DllImport alias.

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"kurotsuke" <ku*******@yaho o.it> wrote in message
news:u8******** *************** *********@4ax.c om...
On Sun, 2 Jan 2005 04:56:06 +0200, "Chris Taylor"
<ch************ *@hotmail.com> wrote:
Hi,

Please do not cross post, I answered this post on the interop group.

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("user 32", SetLastError=tr ue, CharSet=CharSet .Unicode)]
static extern int GetKeyNameTextW (uint lParam, StringBuilder lpString,int nSize);

[DllImport("user 32.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)] static extern uint MapVirtualKeyW( uint uCode, uint uMapType);

private void Form1_KeyDown(o bject sender,
System.Windows .Forms.KeyEvent Args e)
{
StringBuilder sb= new StringBuilder(2 60);

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

label2.Text = sb.ToString();
}

You will notice that I had to use MapVirtualKey to map the key code and thenwhen passing the code to the GetKeyNameText function I performed a shift, inthe 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

--


Sorry for crossposting. After posting here I realized that maybe the
interop newsgroup was better.
I tried your solution and it worked. Will it work on Win98, too?

I'm having a minor problem. When a key on the numpad is pressed, the
description shown is always the numpad one (so I get 9 (tn) and never
get the pageup, pagedown, left, right... codes).
Have you got any idea on how to fix this? The Keycodes are different
for pageUp and (numpad 9)
Thanks again.

Nov 16 '05 #7

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

Similar topics

9
3699
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed. Nothing special, everything works. Except that non-ascii characters are not displayed properly. The data is stored as XML into a text field. When I use pgsql it's displayed good in the terminal. Now I run my script and print data
11
11738
by: singlal | last post by:
Hi, I am currently using DAYOFWEEK function and putting CASE statement to get english day name. For example: SELECT CASE DAYOFWEEK(CURRENT DATE) WHEN 1 THEN 'SUNDAY' WHEN 2 THEN 'MONDAY'
36
3209
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with the needed data. Any ideas? Here's the code in question, see any reason why a command would not trigger the 'kbhit' the first time a serial command is sent?: Thanks! Chuck **************************************************** while(1) //...
1
5556
by: prakash.appidi | last post by:
Hi I am getting the following error when I tried to run the code given below ORA-06550: line 1, column 29: PLS-00553: character set name is not recognized ORA-06550: line 0, column 0: PL/SQL: Compilation unit analysis terminated can anyone explain me why am getting such error and how to resolve it...
7
3343
by: Chad | last post by:
The program: #include <stdio.h> void hello(void) { printf("hello \n"); } void hi(void) { printf("hi \n");
3
2558
by: Jeff | last post by:
I have had this function work perfectly in IE and am trying to get it to work in Firefox. I have seen plenty of questions and answers on the web for how to get and check the key pressed but nothing about setting or changing it. I only want uppercase characters in this case. If the key is a lowercase, it needs to be changed to an uppercase character. IE lets you use keyCode to change the character code. How do I do the equivalent in...
2
1286
by: waltbrad | last post by:
Hello. Been studying Python for about a week now. I did a quick read of the tutorial in the manual and I'm reading Programming Python by Mark Lutz. I'm still getting used to the Python syntax, but I'm able to pretty much follow what is being said. But tonight Lutz was talking about implementing a database on a website and threw out this piece in his code: <tr><th>key<td><input type=text name=key value="%(key)s"> That last bit is...
1
15983
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h and psapi.lib are in this same folder as process.c file, i am getting lonf list of errors from psapi.lib Process.c (from http://voice.mytechnopark.com/viewtopic.php?id=51) #include <windows.h> #include <stdio.h>
185
7134
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will return the file name given a FILE *. Questions: What would be the best name for this function?
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
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
9923
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
8954
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...
0
6722
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();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2860
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.