Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Crazy Scripting : Scriptlet to find Scancode of a Key


  • Please log in to reply
36 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
While experimenting with a keylogger that captures system-wide key strokes using a hook, I transferred a small snippet of the code which works in AHK natively.

Nothing special. Run it and when GUI is in focus press a key and the clipboard will contain the scancode of the key which can be used as hotkey or with Send commands:

SetFormat, Integer, Hex
Gui +ToolWindow -SysMenu +AlwaysOnTop
Gui, Font, s14 Bold, Arial
Gui, Add, Text, w100 h33 vSC 0x201 +Border, {SC000}
Gui, Show,, % "// ScanCode //////////"
Loop 9
  OnMessage( 255+A_Index, "ScanCode" ) ; 0x100 to 0x108
Return

ScanCode( wParam, lParam ) {
 Clipboard := "SC" SubStr((((lParam>>16) & 0xFF)+0xF000),-2) 
 GuiControl,, SC, %Clipboard%
}

:)

Andreone
  • Members
  • 257 posts
  • Last active: Mar 19 2008 05:30 PM
  • Joined: 20 Jul 2007
Thanks, this will be definitively very useful. :)
In fact, maybe the Key List section of the help should have a link on this topic.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

maybe the Key List section of the help should have a link on this topic.


I am not sure whether this the same as the method given in Help doc. :roll:
I have altered the code to monitor more messages, like some keys like <PrtScr> were detected on key-release instead of key-press.

WM_KEYDOWN     = 0x100
WM_KEYUP       = 0x101
WM_CHAR        = 0x102
WM_DEADCHAR    = 0x103
WM_SYSKEYDOWN  = 0x104
WM_SYSKEYUP    = 0x105
WM_SYSCHAR     = 0x106
WM_SYSDEADCHAR = 0x107
WM_KEYLAST     = 0x108

I have only a basic multimedia keyboard and could not test this much!

:)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Posted Image

; Keyboard Hook - [color=indigo]Crude Script to retrieve info on "Mystery keys"[/color] - By Skan
; Created : 15-Jul-2008  // Last Modifed : 15-Jul-2008  
;
; [color=darkred]Note:[/color] [color=red]Yet to be bug-fixed and optimised!!![/color]

#SingleInstance, Force
#Persistent
#InstallKeybdHook
#KeyHistory 20
SetWorkingDir, %A_ScriptDir%

VKeys=
( join
[color=#808080]VK_LBUTTON,VK_RBUTTON,VK_CANCEL,VK_MBUTTON,VK_XBUTTON1,VK_XBUTTON2,,VK_BACK,VK_TAB,Reserve
d,Reserved,VK_CLEAR,VK_RETURN,,,VK_SHIFT,VK_CONTROL,VK_MENU,VK_PAUSE,VK_CAPITAL,VK_HANGUL,
,VK_JUNJA,VK_FINAL,VK_HANJA,,VK_ESCAPE,VK_CONVERT,VK_NONCONVERT,VK_ACCEPT,VK_MODECHANGE,VK
_SPACE,VK_PRIOR,VK_NEXT,VK_END,VK_HOME,VK_LEFT,VK_UP,VK_RIGHT,VK_DOWN,VK_SELECT,VK_PRINT,V
K_EXECUTE,VK_SNAPSHOT,VK_INSERT,VK_DELETE,VK_HELP,VK_0,VK_1,VK_2,VK_3,VK_4,VK_5,VK_6,VK_7,
VK_8,VK_9,,,,,,,,VK_A,VK_B,VK_C,VK_D,VK_E,VK_F,VK_G,VK_H,VK_I,VK_J,VK_K,VK_L,VK_M,VK_N,VK_
O,VK_P,VK_Q,VK_R,VK_S,VK_T,VK_U,VK_V,VK_W,VK_X,VK_Y,VK_Z,VK_LWIN,VK_RWIN,VK_APPS,Reserved,
VK_SLEEP,VK_NUMPAD0,VK_NUMPAD1,VK_NUMPAD2,VK_NUMPAD3,VK_NUMPAD4,VK_NUMPAD5,VK_NUMPAD6,VK_N
UMPAD7,VK_NUMPAD8,VK_NUMPAD9,VK_MULTIPLY,VK_ADD,VK_SEPARATOR,VK_SUBTRACT,VK_DECIMAL,VK_DIV
IDE,VK_F1,VK_F2,VK_F3,VK_F4,VK_F5,VK_F6,VK_F7,VK_F8,VK_F9,VK_F10,VK_F11,VK_F12,VK_F13,VK_F
14,VK_F15,VK_F16,VK_F17,VK_F18,VK_F19,VK_F20,VK_F21,VK_F22,VK_F23,VK_F24,,,,,,,,,VK_NUMLOC
K,VK_SCROLL,OEM,OEM,OEM,OEM,OEM,,,,,,,,,,VK_LSHIFT,VK_RSHIFT,VK_LCONTROL,VK_RCONTROL,VK_LM
ENU,VK_RMENU,VK_BROWSER_BACK,VK_BROWSER_FORWARD,VK_BROWSER_REFRESH,VK_BROWSER_STOP,VK_BROW
SER_SEARCH,VK_BROWSER_FAVORITES,VK_BROWSER_HOME,VK_VOLUME_MUTE,VK_VOLUME_DOWN,VK_VOLUME_UP
,VK_MEDIA_NEXT_TRACK,VK_MEDIA_PREV_TRACK,VK_MEDIA_STOP,VK_MEDIA_PLAY_PAUSE,VK_LAUNCH_MAIL,
VK_LAUNCH_MEDIA_SELECT,VK_LAUNCH_APP1,VK_LAUNCH_APP2,Reserved,Reserved,VK_OEM_1,VK_OEM_PLU
S,VK_OEM_COMMA,VK_OEM_MINUS,VK_OEM_PERIOD,VK_OEM_2,VK_OEM_3,Reserved,Reserved,Reserved,Res
erved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Res
erved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,Reserved,,,,
VK_OEM_4,VK_OEM_5,VK_OEM_6,VK_OEM_7,VK_OEM_8,Reserved,OEM,VK_OEM_102,OEM,OEM,VK_PROCESSKEY
,OEM,VK_PACKET,,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,OEM,VK_ATTN,VK_CRSEL,VK_EX
SEL,VK_EREOF,VK_PLAY,VK_ZOOM,VK_NONAME,VK_PA1,VK_OEM_CLEAR,0xFF[/color]
)
StringSplit, VK, VKeys, `,

DetectHiddenWindows, On
Gui0 := WinExist( A_ScriptFullPath " ahk_class AutoHotkey" )
Gui, Margin,0,0
Gui 1:+LastFound -Sysmenu +AlwaysOnTop +ToolWindow
Gui1 := WinExist()
WinSet, style, -0xC00000, ahk_id %Gui0%
WinMove, ahk_id %Gui0%,,0,-30,640,25
ControlSend,,^{k}, ahk_id %Gui0%
DllCall("SetParent", uint, Gui0, uint,Gui1 ) 
Gui, Font, S10, Courier New
Gui, Add, ListView, Grid w605 r20 -E0x200, VK-Name|VKey|Scan|C|^|Elap|Hotkey Name
LV_ModifyCol(1, "190"), LV_ModifyCol( 2,"60 Center"), LV_ModifyCol(3,"60 Center")
LV_ModifyCol(4, "40 Center"), LV_ModifyCol(5,"40 Center"), LV_ModifyCol( 6,"60 Integer")
LV_ModifyCol(7, "150")
Gui, Show, , % " |<< Keyboard Hook >>|"
OnExit, QuitScript
SetTimer, UpdateListView, 10
Return

UpdateListView:
  If ! ( WinActive( "ahk_id" Gui1 ) OR WinActive( "ahk_id" Gui0 ) )
       Return  
  ControlSend,,{F5}, ahk_id %Gui0%
  ControlGetText, text, Edit1, ahk_id %Gui0%
  VarSetCapacity( hyphens, 109, Asc("-") )
  nText := SubStr( Text,(sPos:=InStr( Text,hyphens )+111), StrLen(Text)-Spos )
  StringReplace,nText,nText,Press [F5] to refresh,,
  StringReplace,nText,nText,%A_Tab%,|,all 
  StringTrimRight,nText,nText,2
  If ( nText = pText )
     Return 
  Gui, 1:Default
  LV_Delete()
  Loop, Parse, nText, `n, `r
   {
     Line := A_LoopField
     Loop,Parse,Line,|
      F%A_Index% := A_LoopField
     VKey := "VK" SubStr(F1,1,2), VK := "0x" SubStr(F1,1,2), 
     VK += 0
     VK := "VK" VK, SC := "SC" SubStr(F1,-2), VKN := %VK% 
     LV_Add( "", VKN, VKey, SC,F2, F3, F4, F5 )  
   }
  pText := nText
Return

QuitScript:
 ExitApp
Return

.. Run the code, make sure script's GUI has keyboard focus, press a key ..

:)
kWo4Lk1.png

kardus
  • Members
  • 17 posts
  • Last active: Jan 30 2017 11:38 PM
  • Joined: 17 Jul 2008
Thanks, useful; but on my system some column are too narrow ...

http://img185.imageshack.us/img185/2932/purrint1645pa5.jpg

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Try altering
Gui, Font, [color=red]S10[/color], Courier New
to S9 or S8

:)
kWo4Lk1.png

kardus
  • Members
  • 17 posts
  • Last active: Jan 30 2017 11:38 PM
  • Joined: 17 Jul 2008
With S8 is ok, thanks!

..:: Free Radical ::..
  • Members
  • 74 posts
  • Last active: May 30 2015 12:13 PM
  • Joined: 20 Sep 2006
amazing. very useful. thanks.

Azerty
  • Members
  • 72 posts
  • Last active: Jan 16 2009 10:08 AM
  • Joined: 19 Dec 2006
This one was needed. thanks Skan. It slipped right into my toolbox :)

PS : a funny bug I got : when pressing CAPS LOCK, the LED keeps flashing until I release CAPS.

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
This is a great little app, thanks. :) I'm not sure if this is just my keyboard, but the scancodes reported for the Multimedia buttons are not the same as key history or your keyboard hook script, although they are quite similar. I tried remapping them with the scancodes returned and they don't work:

ahk			app			key	
12e			02e			vol up
130			030			vol down		
119			019			next
110			010			prev
120			020			mute
124			024			stop
122			022			play/pause
132			032			disconnect
166			066			favourites
165			065			find
16c			06c			mail
15f			05f			sleep
?			  05e			shutdown

"Anything worth doing is worth doing slowly." - Mae West
Posted Image

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

but the scancodes reported for the Multimedia buttons are not the same as key history or your keyboard hook script


Very strange. All the buttons are correctly reported for my USB KB as well as my trashed PS2 Keyboard. :roll:

Are you using any special driver / software that came along with your KB ?
kWo4Lk1.png

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004

Are you using any special driver / software that came along with your KB ?


No, just the standard 101/102-key MS driver.
"Anything worth doing is worth doing slowly." - Mae West
Posted Image

flatron85
  • Members
  • 6 posts
  • Last active: Nov 17 2010 06:57 PM
  • Joined: 13 Oct 2009
awesome. cheers for this I found it very useful re-mapping the 4 hot keys on my BenQ notebook.
(Net, Mail, P1, P2) their numbers are SC173, SC174, SC16c, SC132.

skribb
  • Members
  • 38 posts
  • Last active: Jan 16 2017 01:24 AM
  • Joined: 13 Oct 2009

While experimenting with a keylogger that captures system-wide key strokes using a hook, I transferred a small snippet of the code which works in AHK natively.

Nothing special. Run it and when GUI is in focus press a key and the clipboard will contain the scancode of the key which can be used as hotkey or with Send commands:

SetFormat, Integer, Hex
Gui +ToolWindow -SysMenu +AlwaysOnTop
Gui, Font, s14 Bold, Arial
Gui, Add, Text, w100 h33 vSC 0x201 +Border, {SC000}
Gui, Show,, % "// ScanCode //////////"
Loop 9
  OnMessage( 255+A_Index, "ScanCode" ) ; 0x100 to 0x108
Return

ScanCode( wParam, lParam ) {
 Clipboard := "SC" SubStr((((lParam>>16) & 0xFF)+0xF000),-2) 
 GuiControl,, SC, %Clipboard%
}

:)


A big, huge, gigantic thank you for this.

Guest3000
  • Guests
  • Last active:
  • Joined: --
Get the "scancode" with "skancode" :lol: