The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: FlySky on October 28, 2010, 02:36:29 PM

Title: Converting Virtual Key Codes and Vice Versa
Post by: FlySky on October 28, 2010, 02:36:29 PM
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.
Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: Yuri on October 28, 2010, 03:01:54 PM
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 (http://msdn.microsoft.com/en-us/library/dd375731(VS.85).aspx) table.
Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: FlySky on October 28, 2010, 03:50:23 PM
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.

Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: dedndave on October 28, 2010, 04:22:30 PM
strictly speaking, there is no "ascii presentation" of the function keys,
or the arrow keys, or several others
Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: Yuri on October 28, 2010, 04:40:24 PM
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
Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: MichaelW on October 28, 2010, 09:25:12 PM

;==============================================================================
    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


Title: Re: Converting Virtual Key Codes and Vice Versa
Post by: FlySky on October 29, 2010, 04:04:33 PM
Many thanks for the input to both of you guys. This makes a lot more sense now.