News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Converting Virtual Key Codes and Vice Versa

Started by FlySky, October 28, 2010, 02:36:29 PM

Previous topic - Next topic

FlySky

Hey guys,

I am having a combobox which is having a couple of strings.

The strings it has atm are:

F1
F2
F3

Using the following code I can get the string and copy it into a buffer:

Invoke SendMessage, [ComboboxMemChange], CB_GETCURSEL, NULL, NULL   ;Get selected SEL
Mov [MemChangeComboItemIndex], Eax                           ;copy the itemindex of this setll into ItemIndex
Invoke SendMessage, [ComboboxMemChange], CB_GETLBTEXT, [MemChangeComboItemIndex], Offset MemChangeComboBuffer    ; Get Currently Selected Item Text

Now I want to try to convert this ascii presentation to it corresponding virtual key code:

using this code:

Invoke VkKeyScan, Offset MemChangeComboBuffer  ;Convert ascii into virtual key code (VkKeyScanA). To do it the other way around we can use: ToAsciiEx or ToAscii

The result when having F1 selected in the combobox = 332.

Now I am using the following API to convert this virtual key code into the actual scan code I can use with GetAsyncKeyState

Invoke MapVirtualKey, eax, 0

I tried all uCode types but the result is always 0. What am I doing wrong.

Yuri

VkKeyScan converts single characters and not key names like F1. The VK code of F1 is 112d or 70h and not 332. According to this Virtual-Key Codes table.

FlySky

Hey Yuri,

I know 112d / 70h is the Virtual Key code VK_F1. I assumed the VkKeyScan API was able to convert the ascii presentation of the key to the actual keycode.


dedndave

strictly speaking, there is no "ascii presentation" of the function keys,
or the arrow keys, or several others

Yuri

FlySky
Btw, you are passing VkKeyScan a pointer to a string, but it expects a character code. I've just made the same mistake and got the same result: 332h.  :toothy
This works for me (in GoAsm):

.CODE

Start: USES ebx
    mov eax,"a"
    invoke user32:VkKeyScanA, eax
    mov ebx,eax
    invoke user32:MapVirtualKeyA, ebx, 0
    invoke msvcrt:printf, "VK code: %Xh, SC code: %Xh", ebx, eax
    add esp,0Ch
    ret
ENDU

MichaelW


;==============================================================================
    include \masm32\include\masm32rt.inc
;==============================================================================

MAPTYPE_VK2SC   equ 0
MAPTYPE_SC2VK   equ 1
MAPTYPE_VK2CHAR equ 2
MAPTYPE_SC2VKLR equ 3

;==============================================================================
    .data
        buffer db 100 dup(0)
    .code
;==============================================================================
start:
;==============================================================================
    ;----------------------------------------------
    ; This returns the key character.
    ;----------------------------------------------

    invoke MapVirtualKey, VK_X, MAPTYPE_VK2CHAR
    print hex$(eax),13,10

    ;------------------------------------------------------
    ; This returns zero because there is no key character.
    ;------------------------------------------------------

    invoke MapVirtualKey, VK_F1, MAPTYPE_VK2CHAR
    print hex$(eax),13,10,13,10


    ;------------------------------------------------------------------
    ; VkKeyScan expects a key character, passed directly on the stack.
    ;------------------------------------------------------------------

    print hex$(VK_X),13,10
    invoke VkKeyScan, 'x'
    print hex$(eax),13,10
    mov eax, 'x'
    invoke VkKeyScan, eax
    print hex$(eax),13,10
    push 'x'
    call VkKeyScan
    print hex$(eax),13,10,13,10

    ;-----------------------------------------------------------------------
    ; Not all of the keys have a character, but all of them have a name and
    ; for keys that have a suitable character the name is the character.
    ;-----------------------------------------------------------------------

    invoke MapVirtualKey, VK_X, MAPTYPE_VK2SC
    shl eax, 16     ; shift scan code into bits 16-23
    invoke GetKeyNameText, eax, ADDR buffer, SIZEOF buffer
    print ADDR buffer,13,10

    invoke MapVirtualKey, VK_SPACE, MAPTYPE_VK2SC
    shl eax, 16     ; shift scan code into bits 16-23
    invoke GetKeyNameText, eax, ADDR buffer, SIZEOF buffer
    print ADDR buffer,13,10

    invoke MapVirtualKey, VK_LSHIFT, MAPTYPE_VK2SC
    shl eax, 16     ; shift scan code into bits 16-23
    invoke GetKeyNameText, eax, ADDR buffer, SIZEOF buffer
    print ADDR buffer,13,10

    invoke MapVirtualKey, VK_F1, MAPTYPE_VK2SC
    shl eax, 16     ; shift scan code into bits 16-23
    invoke GetKeyNameText, eax, ADDR buffer, SIZEOF buffer
    print ADDR buffer,13,10,13,10

    inkey "Press any key to exit..."
    exit
;==============================================================================
end start


00000058
00000000

00000058
00000058
00000058
00000058

X
Space
Shift
F1


eschew obfuscation

FlySky

Many thanks for the input to both of you guys. This makes a lot more sense now.