Shoutbox

Get ascii character from virtual key code - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Get ascii character from virtual key code (/showthread.php?tid=70728)

Get ascii character from virtual key code by deAd on 01-15-2007 at 08:18 PM

In an ChatWndEditKeyDown event, it is sometimes necessary to find which key is down. I could do this by directly comparing the virtual key code that was pressed with the one I would like to see, but this could fail on keyboards with different languages.

What I need is a way to find which character was pressed regardless of the keyboard layout that is fast and won't slow down typing...but I can't figure out how.

If there is a better way to do this please tell me :).

EDIT: Maybe the ToAscii and ToAsciiEx functions could be used, but I don't think the event passes enough information to call them.


RE: Get ascii character from virtual key code by matty on 01-15-2007 at 08:45 PM

It is doable using ToAcii, GetKeyboardState, GetKeyboardLayout.


RE: Get ascii character from virtual key code by deAd on 01-15-2007 at 08:47 PM

But will it slow down typing and is there a better way?


RE: Get ascii character from virtual key code by CookieRevised on 01-15-2007 at 09:00 PM

quote:
Originally posted by deAd
...but this could fail on keyboards with different languages.
it isn't 'language' (better: 'keyboard') specific...

What you are referring to are keyboard scancodes which do depend on the keyboard layout, but virtual keycodes (like used in ChatWndEditKeyDown) don't...

search... : "CookieRevised's reply to # key and = key" ;)
RE: Get ascii character from virtual key code by deAd on 01-15-2007 at 09:07 PM

So according to your post, every character has a designated virtual key code and it's safe to compare the key code from the event to the one I want without discrepancies?

EDIT: MSDN says "Used for miscellaneous characters; it can vary by keyboard." and that it's the one I'm looking for on the US standard keyboard...this contradicts what you're saying, no?


RE: Get ascii character from virtual key code by CookieRevised on 01-15-2007 at 09:10 PM

quote:
Originally posted by deAd
So according to your post, every character has a designated virtual key code and it's safe to compare the key code from the event to the one I want without discrepancies?
yes, since every character on your keyboard has a virtual key code (which is the same for everybody).

Note: but not every key code has a character assigned to it, eg: the CTRL key will only have a virtual key code (which is the same for everybody), while the "A" key has both a virtual key code as a character code.

PS: logically, it would have been very doubtfull Patchou would include a function like that which would highly depend on keyboard layout (making it virtually impossible to use such a function to catch keystrokes in scripts)

;)

quote:
Originally posted by deAd
EDIT: MSDN says "Used for miscellaneous characters; it can vary by keyboard." and that it's the one I'm looking for on the US standard keyboard...this contradicts what you're saying, no?
No, that is either about Keyboard SCAN codes...

...Or either about the fact that a keyboard can contain special keys which can be defined by the manufacters. In those few cases, those keys are extremely off standard and should never be used to control a program made for public use. (eg: the VK_OEM_x keys are examples of such keys. The OEM says it all, Original Equipment Manufacturer)

See my linked-to post in the first reply and also "CookieRevised's reply to Virtual Key Code".
RE: Get ascii character from virtual key code by deAd on 01-15-2007 at 09:26 PM

The keycode I am using is a VK_OEM_x code. So in this case it won't be guaranteed to turn out the right key?


RE: Get ascii character from virtual key code by CookieRevised on 01-15-2007 at 09:43 PM

quote:
Originally posted by deAd
The keycode I am using is a VK_OEM_x code. So in this case it won't be guaranteed to turn out the right key?
yep, indeed...

If you're going to use that script for your own, then it's ok. But don't publish it for others to use since those keys might not even be on the user's keyboard.
RE: RE: Get ascii character from virtual key code by deAd on 01-15-2007 at 09:44 PM

quote:
Originally posted by CookieRevised
quote:
Originally posted by deAd
The keycode I am using is a VK_OEM_x code. So in this case it won't be guaranteed to turn out the right key?
yep, indeed...

If you're going to use that script for your own, then it's ok. But don't publish it for others to use since those keys might not even be on the user's keyboard.

I am probably going to publish it but I could remove this feature.

I think I have fixed the problem by comparing the key code with the result of VkKeyScanW and the character's ASCII code :)

EDIT: and it's not a very uncommon key.... I'm fairly certain almost all keyboards have it, as it's used within plus! as well :P
RE: RE: RE: Get ascii character from virtual key code by CookieRevised on 01-15-2007 at 10:01 PM

quote:
Originally posted by deAd
I think I have fixed the problem by comparing the key code with the result of VkKeyScanW and the character's ASCII code :)
No...

And since you speak of "the character's ASCII code", it means that this special key of yours might just be a shortcut of another standard existing key. So, use that one instead.

quote:
Originally posted by deAd
EDIT: and it's not a very uncommon key.... I'm fairly certain almost all keyboards have it, as it's used within plus! as well :P
tbh, very very doubtfull.

-------

What is the key and what is it supposed todo in Plus!?
RE: RE: RE: RE: Get ascii character from virtual key code by deAd on 01-15-2007 at 10:48 PM

quote:
Originally posted by CookieRevised
quote:
Originally posted by deAd
I think I have fixed the problem by comparing the key code with the result of VkKeyScanW and the character's ASCII code :)
No...

And since you speak of "the character's ASCII code", it means that this special key of yours might just be a shortcut of another standard existing key. So, use that one instead.

quote:
Originally posted by deAd
EDIT: and it's not a very uncommon key.... I'm fairly certain almost all keyboards have it, as it's used within plus! as well :P
tbh, very very doubtfull.

-------

What is the key and what is it supposed todo in Plus!?

The key is VK_OEM_4 (0xDB) on US keyboards, which is ASCII 91 (0x5B), or "[". It may not be parsed when it is typed as in the script, but it is used for tags within plus!, and many other projects, so it must be common to be on keyboards.

Why would using VkKeyScanW not work? I provide it with a static ASCII code which never changes (91, or 0x5B) and it provides me with its virtual key code (in my case, VK_OEM_4 or 0xDB). It's then compared to the pressed key code so it should work on all systems that have "[" as one of the keys...

I do not think it's a "shortcut" for another existing key, because MSDN labels only VK_OEM_4 as "[", and the ChatWndEditKeyDown function provides a virtual key code and not an ASCII one...
RE: Get ascii character from virtual key code by CookieRevised on 01-17-2007 at 09:00 AM

quote:
Originally posted by Matty
It is doable using ToAcii, GetKeyboardState, GetKeyboardLayout.

It actually isn't. Also ToAscii also requires a scan code, which isn't provided by Plus!.

quote:
Originally posted by deAd
It may not be parsed when it is typed as in the script, but it is used for tags within plus!, and many other projects, so it must be common to be on keyboards.
It is not because the character [ is used as part of formatting tags, that Plus! detects it as a virtual key. Plus! parses the string you already typed and checks the ascii codes (which can be different than virtual codes). As I know you know... :p

quote:
Originally posted by deAd
The key "[" is VK_OEM_4 (0xDB)
That character/key can be 0xDD on other keyboards for example, also you must to take into account the shift states which Plus! doesn't all support either, so:

quote:
Originally posted by deAd
It's then compared to the pressed key code so it should work on all systems that have "[" as one of the keys...
It wont unfortunatly, because it is an OEM key.

Ps: And on Belgium keyboard pressing "[" will not even trigger a ChatWndEditKeyDown as it involves pressing AltGr (=right ALT) which is not detected by Plus! either.

Not to mention that the API VkKeyScan is something left over from Win95 and since then much things have changed (while the API has not). It wont work ligatures, dead keys, SGCAPS, or anything on the numeric keypad. And the biggest problem is it does not distinguish between the left and the right SHIFT/CTRL/ALT keys, which would be needed to specifically define the proper key. The API VkKeyScan is virtually (get it? 'virtually' :D:p) useless and shouldn't be used....
RE: Get ascii character from virtual key code by deAd on 01-17-2007 at 11:13 PM

Well then...I guess what I'm trying to do won't work. :P