AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Crazy Scripting : Scriptlet to find Scancode of a Key

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Thu Sep 06, 2007 10:09 pm    Post subject: Crazy Scripting : Scriptlet to find Scancode of a Key Reply with quote

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:

Code:
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%
}


Smile


Last edited by SKAN on Fri Sep 07, 2007 8:37 am; edited 1 time in total
Back to top
View user's profile Send private message
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Thu Sep 06, 2007 10:35 pm    Post subject: Reply with quote

Thanks, this will be definitively very useful. Smile
In fact, maybe the Key List section of the help should have a link on this topic.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Fri Sep 07, 2007 8:43 am    Post subject: Reply with quote

Andreone wrote:
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. Rolling Eyes
I have altered the code to monitor more messages, like some keys like <PrtScr> were detected on key-release instead of key-press.

Code:
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!

Smile
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Tue Jul 15, 2008 2:41 pm    Post subject: Keyboard Hook - 90L Reply with quote





Code:
; Keyboard Hook - Crude Script to retrieve info on "Mystery keys" - By Skan
; Created : 15-Jul-2008  // Last Modifed : 15-Jul-2008 
;
; Note: Yet to be bug-fixed and optimised!!!

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

VKeys=
( join
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

)
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 ..

Smile
_________________
Suresh Kumar A N
Back to top
View user's profile Send private message
kardus



Joined: 17 Jul 2008
Posts: 10

PostPosted: Fri Jul 18, 2008 6:59 am    Post subject: Reply with quote

Thanks, useful; but on my system some column are too narrow ...

http://img185.imageshack.us/img185/2932/purrint1645pa5.jpg
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Fri Jul 18, 2008 12:21 pm    Post subject: Reply with quote

Try altering
Code:
Gui, Font, S10, Courier New

to S9 or S8

Smile
_________________
Suresh Kumar A N
Back to top
View user's profile Send private message
kardus



Joined: 17 Jul 2008
Posts: 10

PostPosted: Sat Jul 26, 2008 10:23 pm    Post subject: Reply with quote

With S8 is ok, thanks!
Back to top
View user's profile Send private message
..:: Free Radical ::..



Joined: 20 Sep 2006
Posts: 47

PostPosted: Mon Jul 28, 2008 6:04 pm    Post subject: Reply with quote

amazing. very useful. thanks.
Back to top
View user's profile Send private message
Azerty



Joined: 19 Dec 2006
Posts: 72
Location: France

PostPosted: Thu Jul 31, 2008 7:17 am    Post subject: Reply with quote

This one was needed. thanks Skan. It slipped right into my toolbox Smile

PS : a funny bug I got : when pressing CAPS LOCK, the LED keeps flashing until I release CAPS.
Back to top
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1272

PostPosted: Tue Sep 09, 2008 2:42 am    Post subject: Reply with quote

This is a great little app, thanks. Smile 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:

Code:
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
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Tue Sep 09, 2008 4:30 am    Post subject: Reply with quote

Serenity wrote:
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. Rolling Eyes

Are you using any special driver / software that came along with your KB ?
_________________
Suresh Kumar A N
Back to top
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1272

PostPosted: Tue Sep 09, 2008 9:37 am    Post subject: Reply with quote

SKAN wrote:
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
Back to top
View user's profile Send private message Visit poster's website
flatron85



Joined: 13 Oct 2009
Posts: 2

PostPosted: Tue Oct 13, 2009 5:05 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
skribb



Joined: 13 Oct 2009
Posts: 10

PostPosted: Sun Oct 25, 2009 3:29 pm    Post subject: Re: Crazy Scripting : Scriptlet to find Scancode of a Key Reply with quote

SKAN wrote:
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:

Code:
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%
}


Smile


A big, huge, gigantic thank you for this.
Back to top
View user's profile Send private message
Guest3000
Guest





PostPosted: Sun Oct 25, 2009 4:26 pm    Post subject: Reply with quote

Get the "scancode" with "skancode" Laughing
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group