Jump to content

Hotkey Inputboxes...


Achilles
 Share

Recommended Posts

Alright... After much help from GaryFrost I've got this. I plan to implement it into my Multiple Desktops code but I want to know if it works good. The part I really care about is the last part of the message box that says what the assignment statement is.

For example if the hotkey is Ctrl + Alt + A is should say ^!a Please let me know if that is working for most of the hotkeys.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

HotKeySet('{CAPSLOCK}', '_ExcludeHotkey') 
HotKeySEt('{NUMLOCK}', '_ExcludeHotkey')

Global Const $HKM_SETHOTKEY = $WM_USER + 1
Global Const $HKM_GETHOTKEY = $WM_USER + 2
Global Const $HKM_SETRULES = $WM_USER + 3

Global Const $HOTKEYF_ALT = 0x04
Global Const $HOTKEYF_CONTROL = 0x02
Global Const $HOTKEYF_EXT = 0x80; Extended key
Global Const $HOTKEYF_SHIFT = 0x01

; invalid key combinations
Global Const $HKCOMB_A = 0x8; ALT
Global Const $HKCOMB_C = 0x4; CTRL
Global Const $HKCOMB_CA = 0x40; CTRL+ALT
Global Const $HKCOMB_NONE = 0x1; Unmodified keys
Global Const $HKCOMB_S = 0x2; SHIFT
Global Const $HKCOMB_SA = 0x20; SHIFT+ALT
Global Const $HKCOMB_SC = 0x10; SHIFT+CTRL
Global Const $HKCOMB_SCA = 0x80; SHIFT+CTRL+ALT


$gui_Main = GUICreate('Get Hotkey', 220, 90)

$bt = GUICtrlCreateButton('See Value', 10, 50, 200, 30);
    GUICtrlSetState(-1, $GUI_DEFBUTTON)

$hWnd = _WinAPI_CreateWindowEx (0, 'msctls_hotkey32', '', BitOR($WS_CHILD, $WS_VISIBLE), 10, 10, 200, 25, $gui_Main)

_SendMessage($hWnd, $HKM_SETRULES, _
BitOR($HKCOMB_NONE, $HKCOMB_S), _                   ; invalid key combinations
BitOR(BitShift($HOTKEYF_ALT, -16), BitAND(0, 0xFFFF))); add ALT to invalid entries

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $bt
            $i_HotKey = _SendMessage($hWnd, $HKM_GETHOTKEY)
            $n_Flag = BitShift($i_HotKey, 8); high byte
            $i_HotKey = BitAND($i_HotKey, 0xFF); low byte
            $sz_Flag = ""
            If BitAND($n_Flag, $HOTKEYF_SHIFT) Then $sz_Flag = "SHIFT + "
            If BitAND($n_Flag, $HOTKEYF_CONTROL) Then $sz_Flag = $sz_Flag & " CTRL + "
            If BitAND($n_Flag, $HOTKEYF_ALT) Then $sz_Flag = $sz_Flag & " ALT + "
            Msgbox(0, $sz_Flag & Chr($i_Hotkey), 'Chr(' & $i_Hotkey & ') = ' & Chr($i_Hotkey) & @CRLF & @CRLF & _GetCode($sz_Flag & Chr($i_Hotkey)))
            If $i_Hotkey = 20 or $i_Hotkey = 144 then
                _SendMessage($hWnd, $HKM_SETHOTKEY)
            EndIf
    EndSwitch
WEnd
_WinAPI_DestroyWindow ($hWnd)

Exit

Func _GetCode($string)
    $temp = StringSplit($string, '+')
    
    $lastTerm = StringLower(StringStripWS($temp[Ubound($temp) - 1], 8))

    If StringLen($lastTerm) > 1 then
        If StringLeft($lastTerm, 1) = 'F' then
            $append = '{' & StringUpper($lastTerm) & '}'
        EndIf
    Else
        $temp = StringLower(StringRight($string, 1))
        If $temp = '!' then
            $append = '{PGUP}'
        ElseIf $temp = '"' then
            $append = '{PGDN}'
        ElseIf $temp = '$' then
            $append = '{HOME}'
        ElseIf $temp = '#' then
            $append = '{END}'
        ElseIf $temp = '-' then
            $append = '{INS}'
        Else         
            $append = StringLower(StringRight($string, 1))
        EndIf
    EndIf
    
    $hotkeyAssignment = ''
    If StringInStr($string, 'CTRL') > 0 then
        $hotkeyAssignment &= '^'
    EndIf

    If StringInStr($string, 'SHIFT') > 0 then
        $hotkeyAssignment &= '+'
    EndIf

    If StringInStr($string, 'ALT') > 0 then
        $hotkeyAssignment &= '!'
    EndIf

    $hotkeyAssignment &= $append
    
    Return $hotkeyAssignment
EndFunc

Func _ExcludeHotkey() 
    _SendMessage(_WinAPI_GetFocus(), $HKM_SETHOTKEY)
EndFunc
Edited by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Hi, nice script, but if you want someone to test it properly, you should probably provide all includes that it required :)

I used A3LWinApi.au3 instead of WinApi.au3 (i don't have it i think), but the Shift key wont work :P - or it supose not to work? <_<

And also you need to include <Misc.au3>.

Btw, i did something like this "manualy" here :)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Hi, nice script, but if you want someone to test it properly, you should probably provide all includes that it required :D

I used A3LWinApi.au3 instead of WinApi.au3 (i don't have it i think)

WinApi.au3 is in the newest beta...

but the Shift key wont work :P - or it supose not to work? <_<

And also you need to include <Misc.au3>.

Do you think shift would work? This is programmed so that shift won't work because I didn't think it would be useful...

Btw, i did something like this "manualy" here :)

This doesn't seem very functional on my computer :) Edited by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

WinApi.au3 is in the newest beta...

I see, but not all of as using beta :)

I didn't think it would be usefu

It depends on what you (scripter) need.

This doesn't seem very functional on my computer

It work only with one main hotkey (Shift/Ctrl/Alt) and with numbers keys (1, 2, 3 etc), but it can be modified to other hotkeys, it was just an example :P

Ok, i play around with this a litle, and it seems that «Ctrl Shift '» and «Ctrl Shift `» returned incorrect values: «^+ю» and «^+а» - is this normal?

Edit

And Ctrl , . / \ ] [ ; and so one <_<

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Hi, nice script, but if you want someone to test it properly, you should probably provide all includes that it required :)

I used A3LWinApi.au3 instead of WinApi.au3 (i don't have it i think), but the Shift key wont work :P - or it supose not to work? <_<

And also you need to include <Misc.au3>.

Btw, i did something like this "manualy" here :)

You'll find once the beta goes to release that the A3L Libs will be broken, most of what is in the A3L is now in Beta. Plus the UDF function Naming convention changed in the last 2 betas.

So any script you have using UDFs supplied with AutoIt will most likely be broken when the release comes out.

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • 6 months later...

This is exactly the script I've been looking for, however, I don't like see hotkeys in bold font...how do I make it normal?

Thank you.

[EDIT]

Found the solution in this reply.

Global $hFont = 0
SetFont($hWnd, 8, 400, 0, 'MS SANS SERIF')
Func SendMessage($hWnd, $Msg, $wParam = 0, $lParam = 0)
    $a_Ret = DllCall('user32.dll', 'int', 'SendMessage', _
                     'hwnd', $hWnd, _
                     'int', $Msg, _
                     'int', $wParam, _
                     'int', $lParam)
    Return $a_Ret[0]
EndFunc
; Set/create the font
Func SetFont($hWnd, $nSize, $nWeight, $nAtrribute, $szFont)

    If $hFont = 0 Then
        $hDc = DllCall("user32.dll", "hwnd", "GetDC", "hwnd", $hWnd)
        $nPixel = DllCall("gdi32.dll", "int", "GetDeviceCaps", "hwnd", $hDc[0], "int", 90)
        $nHeight = DllCall("kernel32.dll", "int", "MulDiv", "int", $nSize, "int", $nPixel[0], "int", 72)
        $hFont = DllCall('gdi32.dll', 'hwnd', 'CreateFont', _
                        'int', -$nHeight[0], _
                        'int', 0, _
                        'int', 0, _
                        'int', 0, _
                        'int', $nWeight, _
                        'int', BitAnd($nAtrribute, 2), _
                        'int', BitAnd($nAtrribute, 4), _
                        'int', BitAnd($nAtrribute, 8), _
                        'int', 1, _
                        'int', 0, _
                        'int', 0, _
                        'int', 0, _
                        'int', 0, _
                        'str', $szFont)
    EndIf

; Other possibility to select a font to the control:
;
; $hFont = DllCall('gdi32.dll', 'hwnd', 'GetStockObject', 'int', 11); Get "DEFAULT_GUI_FONT"
;
;   -> font numbers can be :
;   OEM_FIXED_FONT    10
;   ANSI_FIXED_FONT  11
;   ANSI_VAR_FONT      12
;   SYSTEM_FONT      13
;   DEVICE_DEFAULT_FONT 14
;   DEFAULT_PALETTE  15
;   SYSTEM_FIXED_FONT   16
;   DEFAULT_GUI_FONT    17
;
;   ! DeleteFont($hFont) before 'Exit' has to be commented out !
    
    SendMessage($hWnd, $WM_SETFONT, $hFont[0], 1)
EndFunc
Edited by vanowm
Link to comment
Share on other sites

I hit another wall...How to reverse the process of translating code returned by the hotkey input to the AU3 so we can set default key when started?

In my case the hotkeys are set by user and saved into .ini file. Now I need use the hotkey from .ini file and place them into co-responding hotkey input fields when user opens the settings...

Thank you.

Link to comment
Share on other sites

I hit another wall...How to reverse the process of translating code returned by the hotkey input to the AU3 so we can set default key when started?

In my case the hotkeys are set by user and saved into .ini file. Now I need use the hotkey from .ini file and place them into co-responding hotkey input fields when user opens the settings...

Thank you.

Depending on how you save your input... :)

$HotKey = IniRead(blah)
$Hotkey StringReplace($Hotkey,"ALT + ",  "!")
HotKeySet($HotKey,"_HotkeyFunc")

While 1
Sleep(100)
Wend

Func _HotkeyFunc()

EndFunc
Link to comment
Share on other sites

Depending on how you save your input... :)

$HotKey = IniRead(blah)
$Hotkey StringReplace($Hotkey,"ALT + ",  "!")
HotKeySet($HotKey,"_HotkeyFunc")

While 1
Sleep(100)
Wend

Func _HotkeyFunc()

EndFunc
You missunderstood my question :)

The hotkey saved into ini already converted into proper AU3 format (converted by _GetCode function), so I need UE3 type of hotkey strin convert back to the same format as it was originally returned by $i_HotKey = _SendMessage($hWnd, $HKM_GETHOTKEY) line (I guess its a binary code). Once the hotkey converted to that format it can be used to set "default" hotkey displayed when creating the hotkey input form (instead of displaying "none" in the input box).

Link to comment
Share on other sites

It was a nice attempt make hotkeys converter without long and boring arrays, however there are too many keys that have special AU3 tags to replace with, so I see no other way but use array. Anyway I got it working - fixed wrong hotkey after conversion to AU3 (like numpad keys) and added reverse conversion from AU3 format into binary, to predefine key in the input field.

In my tests every key on my keyboard are showing/converted correctly, except for: Win, Backspace, Enter, Space, PrtScrn, Pause, Tab, Escape and Application key buttons, I wasn't able figure out how to use them for hotkeys.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#Include <WindowsConstants.au3>

;HotKeySet('{CAPSLOCK}', '_ExcludeHotkey')
;HotKeySEt('{NUMLOCK}', '_ExcludeHotkey')

Global Const $HKM_SETHOTKEY = $WM_USER + 1
Global Const $HKM_GETHOTKEY = $WM_USER + 2
Global Const $HKM_SETRULES = $WM_USER + 3

Global Const $HOTKEYF_ALT = 0x04
Global Const $HOTKEYF_CONTROL = 0x02
Global Const $HOTKEYF_EXT = 0x80; Extended key
Global Const $HOTKEYF_SHIFT = 0x01

; invalid key combinations
Global Const $HKCOMB_A = 0x8; ALT
Global Const $HKCOMB_C = 0x4; CTRL
Global Const $HKCOMB_CA = 0x40; CTRL+ALT
Global Const $HKCOMB_NONE = 0x1; Unmodified keys
Global Const $HKCOMB_S = 0x2; SHIFT
Global Const $HKCOMB_SA = 0x20; SHIFT+ALT
Global Const $HKCOMB_SC = 0x10; SHIFT+CTRL
Global Const $HKCOMB_SCA = 0x80; SHIFT+CTRL+ALT


Global $HotkeyMap
Dim $HotkeyMap[67][2]
$HotkeyMap[0][0] = "{CAPSLOCK}"
$HotkeyMap[0][1] = 20
$HotkeyMap[1][0] = "{NUMPAD0}"
$HotkeyMap[1][1] = 96
$HotkeyMap[2][0] = "{NUMPAD1}"
$HotkeyMap[2][1] = 97
$HotkeyMap[3][0] = "{NUMPAD2}"
$HotkeyMap[3][1] = 98
$HotkeyMap[4][0] = "{NUMPAD3}"
$HotkeyMap[4][1] = 99
$HotkeyMap[5][0] = "{NUMPAD4}"
$HotkeyMap[5][1] = 100
$HotkeyMap[6][0] = "{NUMPAD6}"
$HotkeyMap[6][1] = 101
$HotkeyMap[7][0] = "{NUMPAD7}"
$HotkeyMap[7][1] = 102
$HotkeyMap[8][0] = "{NUMPAD8}"
$HotkeyMap[8][1] = 103
$HotkeyMap[9][0] = "{NUMPAD8}"
$HotkeyMap[9][1] = 104
$HotkeyMap[10][0] = "{NUMPAD9}"
$HotkeyMap[10][1] = 105
$HotkeyMap[11][0] = "{NUMPADMULT}"
$HotkeyMap[11][1] = 106
$HotkeyMap[12][0] = "{NUMPADADD}"
$HotkeyMap[12][1] = 107
$HotkeyMap[13][0] = "{NUMPADSUB}"
$HotkeyMap[13][1] = 109
$HotkeyMap[14][0] = "{NUMPADDOT}"
$HotkeyMap[14][1] = 110
$HotkeyMap[15][0] = "{NUMPADDIV}"
$HotkeyMap[15][1] = 111
$HotkeyMap[16][0] = "{F1}"
$HotkeyMap[16][1] = 112
$HotkeyMap[17][0] = "{F2}"
$HotkeyMap[17][1] = 113
$HotkeyMap[18][0] = "{F3}"
$HotkeyMap[18][1] = 114
$HotkeyMap[19][0] = "{F4}"
$HotkeyMap[19][1] = 115
$HotkeyMap[20][0] = "{F5}"
$HotkeyMap[20][1] = 116
$HotkeyMap[21][0] = "{F6}"
$HotkeyMap[21][1] = 117
$HotkeyMap[22][0] = "{F7}"
$HotkeyMap[22][1] = 118
$HotkeyMap[23][0] = "{F8}"
$HotkeyMap[23][1] = 119
$HotkeyMap[24][0] = "{F9}"
$HotkeyMap[24][1] = 120
$HotkeyMap[25][0] = "{F10}"
$HotkeyMap[25][1] = 121
$HotkeyMap[26][0] = "{F11}"
$HotkeyMap[26][1] = 122
$HotkeyMap[27][0] = "{F12}"
$HotkeyMap[27][1] = 123
$HotkeyMap[28][0] = "{SCROLLLOCK}"
$HotkeyMap[28][1] = 145

;even though the buttons below don't have AU3 tags, they have different ASCII code
$HotkeyMap[29][0] = ";"
$HotkeyMap[29][1] = 186
$HotkeyMap[30][0] = "="
$HotkeyMap[30][1] = 187
$HotkeyMap[31][0] = "-"
$HotkeyMap[31][1] = 189
$HotkeyMap[32][0] = "."
$HotkeyMap[32][1] = 190
$HotkeyMap[33][0] = "/"
$HotkeyMap[33][1] = 191
$HotkeyMap[34][0] = "`"
$HotkeyMap[34][1] = 192
$HotkeyMap[35][0] = "["
$HotkeyMap[35][1] = 219
$HotkeyMap[36][0] = "\"
$HotkeyMap[36][1] = 220
$HotkeyMap[37][0] = "]"
$HotkeyMap[37][1] = 221
$HotkeyMap[38][0] = "'"
$HotkeyMap[38][1] = 222

$HotkeyMap[39][0] = "{HOME}"
$HotkeyMap[39][1] = 2084
$HotkeyMap[40][0] = "{END}"
$HotkeyMap[40][1] = 2083
$HotkeyMap[41][0] = "{INSERT}"
$HotkeyMap[41][1] = 2093
$HotkeyMap[42][0] = "{PGUP}"
$HotkeyMap[42][1] = 2081
$HotkeyMap[43][0] = "{PGDN}"
$HotkeyMap[43][1] = 2082
$HotkeyMap[44][0] = "{LEFT}"
$HotkeyMap[44][1] = 2085
$HotkeyMap[45][0] = "{RIGHT}"
$HotkeyMap[45][1] = 2087
$HotkeyMap[46][0] = "{UP}"
$HotkeyMap[46][1] = 2086
$HotkeyMap[47][0] = "{DOWN}"
$HotkeyMap[47][1] = 2088
$HotkeyMap[48][0] = "{NUMLOCK}"
$HotkeyMap[48][1] = 2192
$HotkeyMap[49][0] = ","
$HotkeyMap[49][1] = 188

; the following keys are either not currently possible to set, or they are duplicates depends on other system conditions, such as when NumLock is off
$HotkeyMap[50][0] = "{SCROLLLOCK}"
$HotkeyMap[50][1] = 3
$HotkeyMap[51][0] = "{BACKSPACE}"
$HotkeyMap[51][1] = 8
$HotkeyMap[52][0] = "{TAB}"
$HotkeyMap[52][1] = 9
$HotkeyMap[53][0] = "{NUMPAD5}"
$HotkeyMap[53][1] = 12
$HotkeyMap[54][0] = "{ENTER}"
$HotkeyMap[54][1] = 13
$HotkeyMap[55][0] = "{SPACE}"
$HotkeyMap[55][1] = 32
$HotkeyMap[56][0] = "{NUMPAD9}"
$HotkeyMap[56][1] = 33
$HotkeyMap[57][0] = "{NUMPAD3}"
$HotkeyMap[57][1] = 34
$HotkeyMap[58][0] = "{NUMPAD1}"
$HotkeyMap[58][1] = 35
$HotkeyMap[59][0] = "{NUMPAD7}"
$HotkeyMap[59][1] = 36
$HotkeyMap[60][0] = "{NUMPAD4}"
$HotkeyMap[60][1] = 37
$HotkeyMap[61][0] = "{NUMPAD8}"
$HotkeyMap[61][1] = 38
$HotkeyMap[62][0] = "{NUMPAD6}"
$HotkeyMap[62][1] = 39
$HotkeyMap[63][0] = "{NUMPAD2}"
$HotkeyMap[63][1] = 40
$HotkeyMap[64][0] = "{NUMPAD0}"
$HotkeyMap[64][1] = 45
$HotkeyMap[65][0] = "{NUMPADDOT}"
$HotkeyMap[65][1] = 46
$HotkeyMap[66][0] = "\"
$HotkeyMap[66][1] = 226


$gui_Main = GUICreate('Get Hotkey', 220, 90)

$bt = GUICtrlCreateButton('See Value', 10, 50, 200, 30);
    GUICtrlSetState(-1, $GUI_DEFBUTTON)

$hWnd = _WinAPI_CreateWindowEx (0, 'msctls_hotkey32', '', BitOR($WS_CHILD, $WS_VISIBLE), 10, 10, 200, 25, $gui_Main)

_SendMessage($hWnd, $HKM_SETRULES, _
BitOR($HKCOMB_NONE, $HKCOMB_S), _                    ; invalid key combinations
BitOR(BitShift($HOTKEYF_ALT, -16), BitAND(0, 0xFFFF))); add ALT to invalid entries


;Generate random hotkey
$key = ""
If Random(0,1,1) Then $key &= "^"
If Random(0,1,1) Then $key &= "!"
If Random(0,1,1) Then $key &= "+"
If Random(0,1,1) Then
    $key &= $HotkeyMap[Random(0,UBound($HotkeyMap, 1)-1, 1)][0]
Else
    $key &= Chr(Random(Asc("a"), Asc("z"), 1)) ;letter case is irrelevant
EndIf
GUISetState()
MsgBox(0, "Generated default hotkey", $key)

_SendMessage($hWnd, $HKM_SETHOTKEY, HotkeyConvert($key, 1))

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $bt
            $i_HotKey = _SendMessage($hWnd, $HKM_GETHOTKEY)
            Msgbox(0, "Converted hotkey to AU3 format", HotkeyConvert($i_Hotkey))
 ;           If $i_Hotkey = 20 or $i_Hotkey = 144 then ;huh? What's wrong with CapsLock and F3?
 ;               _SendMessage($hWnd, $HKM_SETHOTKEY)
 ;           EndIf
    EndSwitch
WEnd
_WinAPI_DestroyWindow ($hWnd)

Exit
Func HotkeyConvert($hotkey, $type = 0)
    Local $key = ""
    Local $n = UBound($HotkeyMap, 1)-1
    Local $bin = 0
    If $type Then ;reverse convert AU3 to binary
        If StringInStr($hotkey, "+") Then $bin = BitOr($bin, $HOTKEYF_SHIFT)
        If StringInStr($hotkey, "^") Then $bin = BitOr($bin, $HOTKEYF_CONTROL)
        If StringInStr($hotkey, "!") Then $bin = BitOr($bin, $HOTKEYF_ALT)
        $hotkey = StringReplace(StringReplace(StringReplace($hotkey, "!", ""), "^", ""), "+", "") ;Removing any Shift/Ctrl/Alt
        For $i = 0 To $n Step 1
            If $HotkeyMap[$i][0] = $hotkey Then
                $key = $HotkeyMap[$i][1]
                ExitLoop
            EndIf
        Next
        If NOT $key Then
            $key = Asc(StringUpper($hotkey))
        EndIf
        $bin = "0x" & Hex(BitOr(0x100 * $bin, $key))
        Return $bin
    EndIf
  $n_Flag = BitShift($hotkey, 8); high byte
  $i_HotKey = BitAND($hotkey, 0x8FF); low byte this will include Home, Insert, etc without Shift, Ctrl and Alt
  $hotkeyAssignment = ''
  If BitAND($n_Flag, $HOTKEYF_SHIFT) Then $hotkeyAssignment &= '+'
  If BitAND($n_Flag, $HOTKEYF_CONTROL) Then $hotkeyAssignment &= '^'
  If BitAND($n_Flag, $HOTKEYF_ALT) Then $hotkeyAssignment &= '!'
    For $i = 0 TO $n Step 1
        If $HotkeyMap[$i][1] = $i_Hotkey Then
            $key = $HotkeyMap[$i][0]
            ExitLoop
        EndIf
    Next
    If NOT $key Then
    $i_HotKey = BitAND($hotkey, 0xFF)
    $key = StringLower(Chr($i_Hotkey)) ;any letter type of keys should be lower case to avoid confusion as capital "A" = Shift + a
    EndIf
    $hotkeyAssignment &= $key
    Return $hotkeyAssignment
EndFunc

Func _ExcludeHotkey()
    _SendMessage(_WinAPI_GetFocus(), $HKM_SETHOTKEY)
EndFunc
Edited by vanowm
Link to comment
Share on other sites

I'm not really sure what you were doing but this works and I think it's easier:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#Include <WindowsConstants.au3>

Global Const $HKM_SETHOTKEY = $WM_USER + 1
Global Const $HKM_GETHOTKEY = $WM_USER + 2
Global Const $HKM_SETRULES = $WM_USER + 3

Global Const $HOTKEYF_ALT = 0x04
Global Const $HOTKEYF_CONTROL = 0x02
Global Const $HOTKEYF_EXT = 0x80; Extended key
Global Const $HOTKEYF_SHIFT = 0x01

; invalid key combinations
Global Const $HKCOMB_A = 0x8; ALT
Global Const $HKCOMB_C = 0x4; CTRL
Global Const $HKCOMB_CA = 0x40; CTRL+ALT
Global Const $HKCOMB_NONE = 0x1; Unmodified keys
Global Const $HKCOMB_S = 0x2; SHIFT
Global Const $HKCOMB_SA = 0x20; SHIFT+ALT
Global Const $HKCOMB_SC = 0x10; SHIFT+CTRL
Global Const $HKCOMB_SCA = 0x80; SHIFT+CTRL+ALT

$gui_Main = GUICreate('Get Hotkey', 220, 90)

$bt = GUICtrlCreateButton('See Value', 10, 50, 200, 30);
    GUICtrlSetState(-1, $GUI_DEFBUTTON)

$hWnd = _WinAPI_CreateWindowEx (0, 'msctls_hotkey32', '', BitOR($WS_CHILD, $WS_VISIBLE), 10, 10, 200, 25, $gui_Main)

_SendMessage($hWnd, $HKM_SETRULES, _
BitOR($HKCOMB_NONE, $HKCOMB_S), _                  ; invalid key combinations
BitOR(BitShift($HOTKEYF_ALT, -16), BitAND(0, 0xFFFF))); add ALT to invalid entries

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $bt
            $i_HotKey = _SendMessage($hWnd, $HKM_GETHOTKEY)
            Msgbox(0, "Converted hotkey to AU3 format", _GetCode($hWnd))
    EndSwitch
WEnd
_WinAPI_DestroyWindow ($hWnd)

Exit

Func _GetCode($hWnd)
    $i_HotKey = _SendMessage($hWnd, $HKM_GETHOTKEY)
    $n_Flag = BitShift($i_HotKey, 8); high byte
    $i_HotKeyNew = BitAND($i_HotKey, 0xFF); low byte
    $sz_Flag = ""

    $subtract = 0
    If BitAnd($n_Flag, $HOTKEYF_CONTROL) Then 
        $sz_Flag &= "CTRL + "
        $subtract += 512
    EndIf
        
    If BitAnd($n_Flag, $HOTKEYF_SHIFT) Then 
        $sz_Flag &= " SHIFT + "
        $subtract += 256
    EndIf
        
    If BitAnd($n_Flag, $HOTKEYF_ALT) Then 
        $sz_Flag &= " ALT + "
        $subtract += 1024
    EndIf 
    
    If $i_Hotkey - $subtract > 90 then 
        For $index = 96 to 105 
            If $i_Hotkey - $subtract = $index then $sz_Flag &= 'NUM ' & $index - 96
        Next
        
        
        For $index = 112 to 123 
            If $i_Hotkey - $subtract = $index then 
                $sz_Flag &= ' F' & $index - 111
            EndIf
        Next 
        
        If $i_Hotkey - $subtract >= 2081 and $i_Hotkey - $subtract <= 2093 then 
            $sz_Flag &= Chr($i_hotkeyNew)
        EndIf
    Else 
        $sz_Flag &= Chr($i_Hotkey - $subtract)
    EndIf 

    $string = $sz_Flag
                
    $temp = StringSplit($string, '+')
    
    $lastTerm = StringLower(StringStripWS($temp[Ubound($temp) - 1], 8))

    If StringLen($lastTerm) > 1 then
        If StringLeft($lastTerm, 1) = 'F' then
            $append = '{' & StringUpper($lastTerm) & '}'
        ElseIf StringLeft($lastTerm, 3) = 'NUM' then 
            $append = '{NUMPAD' & StringRight($lastTerm, 1) & '}' 
        EndIf
    Else
        $temp = StringLower(StringRight($string, 1))
        If $temp = '!' then
            $append = '{PGUP}'
        ElseIf $temp = '"' then
            $append = '{PGDN}'
        ElseIf $temp = '$' then
            $append = '{HOME}'
        ElseIf $temp = '#' then
            $append = '{END}'
        ElseIf $temp = '-' then
            $append = '{INS}'
        Else         
            $append = StringLower(StringRight($string, 1))
        EndIf
    EndIf
    
    $hotkeyAssignment = ''
    If StringInStr($string, 'CTRL') > 0 then
        $hotkeyAssignment &= '^'
    EndIf

    If StringInStr($string, 'SHIFT') > 0 then
        $hotkeyAssignment &= '+'
    EndIf

    If StringInStr($string, 'ALT') > 0 then
        $hotkeyAssignment &= '!'
    EndIf
        
    Return $hotkeyAssignment & $append
EndFunc

Func _ExcludeHotkey()
    _SendMessage(_WinAPI_GetFocus(), $HKM_SETHOTKEY)
EndFunc
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Oh, I see your point, that would work too and it looks nicer.

However your code is missing support for the following keys:

Num /, Num *, Num -, Num +, Num Del, CapsLock, ScrollLock and: ; = - , . / ` [ \ ] '

I just realized that NumLock state is affecting the hotkey code returned by the input. For example 0x00000023 and 0x00000061 are both Num 1 while 0x00000061 is when NumLock is off

Also, why do you convert binary code (0x00000742) to text (CTRL+SHIFT+ALT+:) and then converting that text to AU3 codes (^+!:), one extra not needed step :)

P.S.

I've updated the array above, you can see the binary code for the rest of the keys, included Numpad keys when NumLock is off :P

Or you can use this list as reference what binary code produces which key (it only covers codes from 0x0 to 0xFF):

0x00000008=8=Backspace
0x00000009=9=Tab

0x0000000C=12=Num 5
0x0000000D=13=Enter

0x00000010=16=Shift
0x00000011=17=Ctrl
0x00000012=18=Alt

0x00000014=20=Caps Lock

0x0000001B=27=Esc

0x00000020=32=Space
0x00000021=33=Num 9
0x00000022=34=Num 3
0x00000023=35=Num 1
0x00000024=36=Num 7
0x00000025=37=Num 4
0x00000026=38=Num 8
0x00000027=39=Num 6
0x00000028=40=Num 2

0x0000002C=44=Sys Req
0x0000002D=45=Num 0
0x0000002E=46=Num Del

0x00000030=48=0
0x00000031=49=1
0x00000032=50=2
0x00000033=51=3
0x00000034=52=4
0x00000035=53=5
0x00000036=54=6
0x00000037=55=7
0x00000038=56=8
0x00000039=57=9

0x00000041=65=A
0x00000042=66=B
0x00000043=67=C
0x00000044=68=D
0x00000045=69=E
0x00000046=70=F
0x00000047=71=G
0x00000048=72=H
0x00000049=73=I
0x0000004A=74=J
0x0000004B=75=K
0x0000004C=76=L
0x0000004D=77=M
0x0000004E=78=N
0x0000004F=79=O
0x00000050=80=P
0x00000051=81=Q
0x00000052=82=R
0x00000053=83=S
0x00000054=84=T
0x00000055=85=U
0x00000056=86=V
0x00000057=87=W
0x00000058=88=X
0x00000059=89=Y
0x0000005A=90=Z

0x00000060=96=Num 0
0x00000061=97=Num 1
0x00000062=98=Num 2
0x00000063=99=Num 3
0x00000064=100=Num 4
0x00000065=101=Num 6
0x00000066=102=Num 7
0x00000067=103=Num 8
0x00000068=104=Num 8
0x00000069=105=Num 9
0x0000006A=106=Num *
0x0000006B=107=Num +

0x0000006D=109=Num -
0x0000006E=110=Num Del
0x0000006F=111=Num /
0x00000070=112=F1
0x00000071=113=F2
0x00000072=114=F3
0x00000073=115=F4
0x00000074=116=F5
0x00000075=117=F6
0x00000076=118=F7
0x00000077=119=F8
0x00000078=120=F9
0x00000079=121=F10
0x0000007A=122=F11
0x0000007B=123=F12

0x00000090=144=Num Lock
0x00000091=145=Scroll Lock

0x000000A0=160=Shift
0x000000A1=161=Right Shift
0x000000A2=162=Ctrl
0x000000A3=163=Ctrl
0x000000A4=164=Alt
0x000000A5=165=Alt

0x000000AC=172=M
0x000000AD=173=D
0x000000AE=174=C
0x000000AF=175=B
0x000000B0=176=P
0x000000B1=177=Q
0x000000B2=178=J
0x000000B3=179=G

0x000000B7=183=F

0x000000BA=186=;
0x000000BB=187==
0x000000BC=188=,
0x000000BD=189=-
0x000000BE=190=.
0x000000BF=191=/
0x000000C0=192=`

0x000000C2=194=F15

0x000000DB=219=[
0x000000DC=220=\
0x000000DD=221=]
0x000000DE=222='

0x000000E2=226=\
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...