Jump to content

Virtual Key Code (UDF)


Belini
 Share

Recommended Posts

I made a UDF to return the code for each button to dispense with the table of key codes.

_keycodes.au3

; #FUNCTION# =====================================================================================
; Name...........: _keycodes
; Language ......: English
; Description ...: Return the key code
; Syntax.........: $variable = _KeyCodes($sInputKey)
; Parameters ....: key
; Return values..: Success    - Virtual Key Code
;                 Failure     - @ Error returns "0" if the code is not listed
; Author ........: Belini
; Country.........:Brazil
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================

#CS
;#EXAMPLE# =======================================================================================
#include <WinAPI.au3>
;#include <_keycodes.au3>
Const $MASCARA = 0x8000
Const $tecla = _keycodes("a")

MsgBox('', '', "The key code is  " & '"' & $tecla & '"')
While 1
    $estado = _WinAPI_GetAsyncKeyState($tecla)
    If BitAND($estado, $MASCARA) Then MsgBox('', '', "The key was pressed.")
WEnd
; ================================================================================================
#CE


Func _KeyCodes($sInputKey)
        Local $iRetCode = 0

        If Not IsString($sInputKey) Then Return SetError(1, 0, $iRetCode)

        Switch $sInputKey
                Case "a"
                        $iRetCode = 0x41 ; ==>  A
                Case "b"
                        $iRetCode = 0x42 ; ==>  B
                Case "c"
                        $iRetCode = 0x43 ; ==>  C
                Case "d"
                        $iRetCode = 0x44 ; ==>  D
                Case "e"
                        $iRetCode = 0x45 ; ==>  E
                Case "f"
                        $iRetCode = 0x46 ; ==>  F
                Case "g"
                        $iRetCode = 0x47 ; ==>  G
                Case "h"
                        $iRetCode = 0x48 ; ==>  H
                Case "i"
                        $iRetCode = 0x49 ; ==>  I
                Case "j"
                        $iRetCode = 0x4A ; ==>  J
                Case "k"
                        $iRetCode = 0x4B ; ==>  K
                Case "l"
                        $iRetCode = 0x4C ; ==>  L
                Case "m"
                        $iRetCode = 0x4D ; ==>  M
                Case "n"
                        $iRetCode = 0x4E ; ==>  N
                Case "o"
                        $iRetCode = 0x4F ; ==>  O
                Case "p"
                        $iRetCode = 0x50 ; ==>  P
                Case "q"
                        $iRetCode = 0x51 ; ==>  Q
                Case "r"
                        $iRetCode = 0x52 ; ==>  R
                Case "s"
                        $iRetCode = 0x53 ; ==>  S
                Case "t"
                        $iRetCode = 0x54 ; ==>  T
                Case "u"
                        $iRetCode = 0x55 ; ==>  U
                Case "v"
                        $iRetCode = 0x56 ; ==>  V
                Case "w"
                        $iRetCode = 0x57 ; ==>  W
                Case "x"
                        $iRetCode = 0x58 ; ==>  X
                Case "y"
                        $iRetCode = 0x59 ; ==>  Y
                Case "z"
                        $iRetCode = 0x5A ; ==>  Z
                Case "0"
                        $iRetCode = 0x30 ; ==>  0
                Case "1"
                        $iRetCode = 0x31 ; ==>  1
                Case "2"
                        $iRetCode = 0x32 ; ==>  2
                Case "3"
                        $iRetCode = 0x33 ; ==>  3
                Case "4"
                        $iRetCode = 0x34 ; ==>  4
                Case "5"
                        $iRetCode = 0x35 ; ==>  5
                Case "6"
                        $iRetCode = 0x36 ; ==>  6
                Case "7"
                        $iRetCode = 0x37 ; ==>  7
                Case "8"
                        $iRetCode = 0x38 ; ==>  8
                Case "9"
                        $iRetCode = 0x39 ; ==>  9
                Case "Num *"
                        $iRetCode = 0x6A ; ==>  Num *
                Case "Num 0"
                        $iRetCode = 0x60 ; ==>  Num 0
                Case "Num 1"
                        $iRetCode = 0x61 ; ==>  Num 1
                Case "Num 2"
                        $iRetCode = 0x62 ; ==>  Num 2
                Case "Num 3"
                        $iRetCode = 0x63 ; ==>  Num 3
                Case "Num 4"
                        $iRetCode = 0x64 ; ==>  Num 4
                Case "Num 5"
                        $iRetCode = 0x65 ; ==>  Num 5
                Case "Num 6"
                        $iRetCode = 0x66 ; ==>  Num 6
                Case "Num 7"
                        $iRetCode = 0x67 ; ==>  Num 7
                Case "Num 8"
                        $iRetCode = 0x68 ; ==>  Num 8
                Case "Num 9"
                        $iRetCode = 0x69 ; ==>  Num 9
                Case "Num ."
                        $iRetCode = 0x6E ; ==>  Num .
                Case "Num /"
                        $iRetCode = 0x6F ; ==>  Num /
                Case "Num +"
                        $iRetCode = 0x6B ; ==>  Num +
                Case "Num -"
                        $iRetCode = 0x6D ; ==>  Num -
                Case "F1"
                        $iRetCode = 0x70 ; ==>  F1
                Case "F2"
                        $iRetCode = 0x71 ; ==>  F2
                Case "F3"
                        $iRetCode = 0x72 ; ==>  F3
                Case "F4"
                        $iRetCode = 0x73 ; ==>  F4
                Case "F5"
                        $iRetCode = 0x74 ; ==>  F5
                Case "F6"
                        $iRetCode = 0x75 ; ==>  F6
                Case "F7"
                        $iRetCode = 0x76 ; ==>  F7
                Case "F8"
                        $iRetCode = 0x77 ; ==>  F8
                Case "F9"
                        $iRetCode = 0x78 ; ==>  F9
                Case "F10"
                        $iRetCode = 0x79 ; ==>  F10
                Case "F11"
                        $iRetCode = 0x7A ; ==>  F11
                Case "F12"
                        $iRetCode = 0x7B ; ==>  F12
                Case "Middle mouse"
                        $iRetCode = 0x04 ; ==>  Middle mouse
                Case "Right mouse"
                        $iRetCode = 0x02 ; ==>  Right mouse
                Case "Left mouse"
                        $iRetCode = 0x01 ; ==>  Left mouse
                Case "Mail"
                        $iRetCode = 0xB4 ; ==>  Mail
                Case "Pause"
                        $iRetCode = 0x13 ; ==>  Pause
                Case "Media"
                        $iRetCode = 0xB5 ; ==>  Media
                Case "Next Track"
                        $iRetCode = 0xB0 ; ==>  Next Track
                Case "Previous Track"
                        $iRetCode = 0xB1 ; ==>  Previous Track
                Case "Right Ctrl"
                        $iRetCode = 0xA3 ; ==>  Right Ctrl
                Case "Right Alt"
                        $iRetCode = 0xA5 ; ==>  Right Alt
                Case "Scrol Lock"
                        $iRetCode = 0x91 ; ==>  Scrol Lock
                Case "Print Screen"
                        $iRetCode = 0x2C ; ==>  Print Screen
                Case "Right Shift"
                        $iRetCode = 0xA1 ; ==>  Right Shift
                Case "Left Win"
                        $iRetCode = 0x5B ; ==>  Left Win
                Case "Right Win"
                        $iRetCode = 0x5C ; ==>  Right Win
                Case "Sleep"
                        $iRetCode = 0x5F ; ==>  Sleep
                Case "Context Menu"
                        $iRetCode = 0x5D ; ==>  Context Menu
                Case "ctrl alt Break"
                        $iRetCode = 0x03 ; ==>  ctrl alt Break
                Case "Browser Favorites"
                        $iRetCode = 0xAB ; ==>  Browser Favorites
                Case "Browser Home"
                        $iRetCode = 0xAC ; ==>  Browser Home
                Case "Browser Refresh"
                        $iRetCode = 0xA8 ; ==>  Browser Refresh
                Case "Browser Stop"
                        $iRetCode = 0xA9 ; ==>  Browser Stop
                Case "Browser Forward"
                        $iRetCode = 0xA7 ; ==>  Browser Forward
                Case "Browser Back"
                        $iRetCode = 0xA6 ; ==>  Browser Back
                Case "Browser Search"
                        $iRetCode = 0xAA ; ==>  Browser Search
                Case "Play"
                        $iRetCode = 0xB3 ; ==>  Play
                Case "Stop"
                        $iRetCode = 0xB2 ; ==>  Stop
                Case "Volume Up"
                        $iRetCode = 0xAF ; ==>  Volume Up
                Case "Volume Down"
                        $iRetCode = 0xAE ; ==>  Volume Down
                Case "Mute"
                        $iRetCode = 0xAD ; ==>  Mute
                Case "Page Up"
                        $iRetCode = 0x21 ; ==>  Page Up
                Case "Page Down"
                        $iRetCode = 0x22 ; ==>  Page Down
                Case "Num Lock"
                        $iRetCode = 0x90 ; ==>  Num Lock
                Case "Caps Lock"
                        $iRetCode = 0x14 ; ==>  Caps Lock
                Case "Left Ctrl"
                        $iRetCode = 0xA2 ; ==>  Left Ctrl
                Case "Left Alt"
                        $iRetCode = 0xA4 ; ==>  Left Alt
                Case "Left Shift"
                        $iRetCode = 0xA0 ; ==>  Left Shift
                Case "Space"
                        $iRetCode = 0x20 ; ==>  Space
                Case "Backspace"
                        $iRetCode = 0x08 ; ==>  Backspace
                Case "Delete"
                        $iRetCode = 0x2E ; ==>  Delete
                Case "Insert"
                        $iRetCode = 0x2D ; ==>  Insert
                Case "Up"
                        $iRetCode = 0x26 ; ==>  Up
                Case "Down"
                        $iRetCode = 0x28 ; ==>  Down
                Case "Left"
                        $iRetCode = 0x25 ; ==>  Left
                Case "Right"
                        $iRetCode = 0x27 ; ==>  Right
                Case "Tab"
                        $iRetCode = 0x09 ; ==>  Tab
                Case "Esc"
                        $iRetCode = 0x1B ; ==>  Esc
                Case "End"
                        $iRetCode = 0x23 ; ==>  End
                Case "Home"
                        $iRetCode = 0x24 ; ==>  Home
                Case "Enter"
                        $iRetCode = 0x0D ; ==>  Enter
                Case "["
                        $iRetCode = 0xDD ; ==>  [
                Case "]"
                        $iRetCode = 0xDC ; ==>  ]
                Case ""
                        $iRetCode = 0xE2 ; ==>  
                Case ";"
                        $iRetCode = 0xBF ; ==>  ;
                Case "="
                        $iRetCode = 0xBB ; ==>  =
                Case "~"
                        $iRetCode = 0xDE ; ==>  ~
                Case "´"
                        $iRetCode = 0xDB ; ==>  ´
                Case "'"
                        $iRetCode = 0xC0 ; ==>  '
                Case Else
                        Return SetError(1, 0, $iRetCode)
        EndSwitch

        If $iRetCode = 0 Then Return SetError(1, 0, $iRetCode)

        Return $iRetCode
EndFunc   ;==>_KeyCodes

Example:

#include <WinAPI.au3>
#include "_KeyCodes.au3"

Global Const $MASCARA = 0x8000

Global $sTecla = "a", _ ; Set the key...
                $iEstado, _
                $sCode = _KeyCodes($sTecla)

MsgBox(4096, "_KeyCode", 'The key code "' & $sTecla & '" is: ' & $sCode & @CRLF & _
                "The program will be waiting for her down." & @CRLF & @CRLF & "Click [OK] to continue...")

While 1
        $iEstado = _WinAPI_GetAsyncKeyState($sCode)
        If BitAND($iEstado, $MASCARA) Then MsgBox(4096, "_KeyCode", 'The key "' & $sTecla & '" was pressed.')
sleep(20)
WEnd
Edited by Belini
Link to comment
Share on other sites

Just for the heck of it, you should add a sleep in the while loop so it won't use up an entire CPU in the example.

While 1 
  sleep(10)
$iEstado = _WinAPI_GetAsyncKeyState($sCode)     
If BitAND($iEstado, $MASCARA) Then MsgBox(4096, "_KeyCode", 'The key "' &amp; $sTecla &amp; '" was pressed.')
WEnd

Also, the same things with less code is in the example, you just need to tweak it a little.

<snip>
Edited by Melba23
Code removed
Link to comment
Share on other sites

  • Moderators

THAT1ANONYMOUSEDUDE,

Your code is too close to a real keylogger - so it has vanished. We all know it works, but please do not post it again - Jon does not approve. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

THAT1ANONYMOUSEDUDE,

Your code is too close to a real keylogger - so it has vanished. We all know it works, but please do not post it again - Jon does not approve. :oops:

M23

Oh god, I had a suspicion this was a probability. Sorry about that, I would have thought detecting only one character would have not skimmed close enough to the posted rules.
Link to comment
Share on other sites

  • Moderators

THAT1ANONYMOUSEDUDE,

Suspicion confirmed! :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Just for the heck of it, you should add a sleep in the while loop so it won't use up an entire CPU in the example.

It was distracting me, thanks for reminding me

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...