Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages
Started by RaynorPao
Wed, 26 Mar 2003 07:54
Key Name to Virtual Key Code
Author: RaynorPao
Date: Wed, 26 Mar 2003 07:54
Date: Wed, 26 Mar 2003 07:54
12 lines
1062 bytes
1062 bytes
Hi all: Is there an API can convert Key Name to Virtual Key Code? For example: char sz[16]="F1"; int i=KeyNameToVirtualKeyCode(sz); The value of i will be VK_F1. Thanks. RaynorPao.
Re: Key Name to Virtual Key Code
Author: Vladimir Afanasyev
Date: Wed, 26 Mar 2003 09:13
Date: Wed, 26 Mar 2003 09:13
20 lines
1434 bytes
1434 bytes
Hi, "RaynorPao" <RaynorPao.nospam@nospam.pchome.com.tw> wrote: >Hi all: > >Is there an API can convert Key Name to Virtual Key Code? >For example: > >char sz[16]="F1"; >int i=KeyNameToVirtualKeyCode(sz); > >The value of i will be VK_F1. You can write this function. If you open Win32 Programmer's Reference Help (page Search) and try to find next: "Virtual-Key Codes" you find table of Vitual Codes and mouse and keyboard. Vladimir.
Re: Key Name to Virtual Key Code
Author: Rodolfo Frino - Stargate Softwar...
Date: Wed, 26 Mar 2003 12:13
Date: Wed, 26 Mar 2003 12:13
72 lines
2971 bytes
2971 bytes
There is no API to do that, that I'm aware of.
You can do it like that or you can use a map,
something like this (untested) code
#include <map>
std::map<String, unsigned int> VirtualKeyMap;
unsigned int VirtualKey_NameToCode(String VKName)
{
return VirtualKeyMap[VKName];
}
void Set_VirtualKeyMap()
{
VirtualKeyMap["VK_LBUTTON" ] = 0x01;
VirtualKeyMap["VK_RBUTTON" ] = 0x02;
VirtualKeyMap["VK_CANCEL" ] = 0x03;
VirtualKeyMap["VK_MBUTTON" ] = 0x04;
VirtualKeyMap["VK_XBUTTON1" ] = 0x05;
VirtualKeyMap["VK_XBUTTON2"] = 0x06;
//VirtualKeyMap["" ] = 07;
VirtualKeyMap["VK_BACK" ] = 0x08;
VirtualKeyMap["VK_TAB" ] = 0x09;
//VirtualKeyMap["" ] = 0A;
//VirtualKeyMap["" ] = 0B;
VirtualKeyMap["VK_CLEAR" ] = 0x0C;
VirtualKeyMap["VK_RETURN" ] = 0x0D;
//VirtualKeyMap["" ] = 0E;
//VirtualKeyMap["" ] = 0F;
VirtualKeyMap["VK_SHIFT" ] = 0x10;
VirtualKeyMap["VK_CONTROL" ] = 0x11;
VirtualKeyMap["VK_MENU" ] = 0x12;
VirtualKeyMap["VK_PAUSE" ] = 0x13;
VirtualKeyMap["VK_CAPITAL" ] = 0x14;
VirtualKeyMap["VK_KANA" ] = 0x15;
VirtualKeyMap["VK_HANGUEL" ] = 0x15;
VirtualKeyMap["VK_HANGUL" ] = 0x15;
//VirtualKeyMap["" ] = 16;
//All the rest
}
//---------------------------------------------------------------------
__fastcall TForm1 :: TForm1(TComponent* Owner)
: TForm(Owner)
{
Set_VirtualKeyMap();
}
void __fastcall TForm1:: Button1Click(TObject *Sender)
{
ShowMessage(String(VirtualKey_NameToCode("VK_RETURN")));
ShowMessage(String(VirtualKey_NameToCode("VK_SHIFT")));
}
Rodolfo
"RaynorPao" <RaynorPao.nospam@nospam.pchome.com.tw> wrote in message
news:3e8158e9@newsgroups.borland.com...
> Hi all:
>
> Is there an API can convert Key Name to Virtual Key Code?
> For example:
>
> char sz[16]="F1";
> int i=KeyNameToVirtualKeyCode(sz);
>
> The value of i will be VK_F1.
>
> Thanks.
> RaynorPao.
>
>
Re: Key Name to Virtual Key Code
Author: Troels Jakobsen
Date: Wed, 26 Mar 2003 18:07
Date: Wed, 26 Mar 2003 18:07
40 lines
1935 bytes
1935 bytes
You can get the key name from the virtual code, but not the
other way round.
You can of course create a method that uses an array and
some mapping logic, but the easiest is to use brute force.
Say you have method VirtualKeyToKeyName that returns the
key name. You can then create a method, KeyNameToVirtualKey,
which calls VirtualKeyToKeyName until it returns a matching
key name:
string VirtualKeyToKeyName(int virtualKey) {
// return key name
}
int KeyNameToVirtualKey(string keyName) {
for (int i=0x08; i<0xFF; i++) {
if (AnsiCompareText(keyName, VirtualKeyToKeyName(i)) == 0) {
return i;
}
}
return -1;
}
This approach is a little CPU-intensive. On the positive side
it's simple, and it's independent of the user's language settings.
Troels
RaynorPao wrote in message <3e8158e9@newsgroups.borland.com>...
>Hi all:
>
>Is there an API can convert Key Name to Virtual Key Code?
>For example:
>
>char sz[16]="F1";
>int i=KeyNameToVirtualKeyCode(sz);
>
>The value of i will be VK_F1.
Re: Key Name to Virtual Key Code
Author: RaynorPao
Date: Thu, 27 Mar 2003 03:41
Date: Thu, 27 Mar 2003 03:41
7 lines
1119 bytes
1119 bytes
Hi Vladimir, Rodolfo, Troels: Thanks for your reply. I think I'd better to write this function myself. Thanks. RaynorPao.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads