Jump to content
Sign in to follow this  

User defined Hotkeys

Recommended Posts

Ok, here's what I'm trying to do (script is programmed as OnEvent)...

I have a GuiCtrlCreateInput designated for the definition of a hotkey, what I want to do is when the user presses the keys they want to use for the hotkey it will appear in the control (ie ... user presses CTRL+A as the hotkey, then CTRL+A appears in the control).

But ... if they just press CTRL or ALT or SHIFT and release the field is cleared out. And no other input is allowed after a "non-special" key is pressed (ie, with the above example ... after the A key is pressed no other key will appear in the input box.

Yep ... I am still a noob. But I do realize this may creep into "that which must not be named area" :) (keylogger).

I've looked at the _IsPressed UDF, but I can't wrap my head around how to use it in this case.

Share this post

Link to post
Share on other sites

I use this UDF to allow the user to select a hotkey:

(Simpler to code)

; Function Name:    _SetHotKeyDialog()
; Description:    Opens a dialog for people to choose a hotkey. In format: modifier+key
; Parameter(s):  $hotkeytext     - Purpourse of hotkey which you ask the user to set
;                  $oldhotkeymod   - "Default" hotkey mod.
;                                    "alt", "ctrl", "winkey", "alt+ctrl", "alt+winkey", "ctrl+winkey", "alt+ctrl+winkey" - All lowercase!
;                  $oldhotkeykey   - "Defalut" hotkey key. a-z(lowercase), 0-9, {F1}-{F12} (excluding {F5}), all {} keys except for {CTRLBREAK}
; Requirement(s):   AutoIt
; Return Value(s):  On Success - Returns array
;                               $return[0] is modifier            (eg. "alt+ctrl")
;                               $return[1] is key                  (eg. "s")
;                               $return[2] is hotkey in send format (eg. "!^s").
;                  On Failure - Returns nothing and sets @ERROR = 1
; Author(s):        nfwu (aka TechDude)   me@techdudeonline.tk
Func _SetHotKeyDialog($hotkeytext, $oldhotkeymod, $oldhotkeykey)
    #include <GuiConstants.au3>
;If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
;If not IsDeclared('WS_OVERLAPPEDWINDOW') Then Global Const $WS_OVERLAPPEDWINDOW    = 0x00CF0000
;if Not IsDeclared('WS_VISIBLE') Then Global Const $WS_VISIBLE  = 0x10000000
    If Not IsDeclared('TDS_SFKD_GUI') Then
        $TDS_SFKD_GUI = GUICreate("MyGUI", 195, 150, (@DesktopWidth - 195) / 2, (@DesktopHeight - 135) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
        $TDS_SFKD_Label1 = GUICtrlCreateLabel("You are modifying the HotKey for:", 10, 0, 180, 20)
        $TDS_SFKD_ComboMod = GUICtrlCreateCombo("", 80, 50, 100, 21)
        $TDS_SFKD_ComboKey = GUICtrlCreateCombo("", 80, 100, 100, 21)
        $TDS_SFKD_Label2 = GUICtrlCreateLabel("AND", 120, 80, 50, 20)
        $TDS_SFKD_Label3 = GUICtrlCreateLabel("Modifier:", 30, 50, 40, 20)
        $TDS_SFKD_Label4 = GUICtrlCreateLabel("Key:", 50, 100, 20, 20)
        $TDS_SFKD_Label5 = GUICtrlCreateLabel("<purpose of hotkey>", 10, 20, 180, 20)
        $TDS_SFKD_button = GUICtrlCreateButton( "OK", 10, 130, 180, 20)
    GUICtrlSetData($TDS_SFKD_Label5, $hotkeytext)
    GUICtrlSetData($TDS_SFKD_ComboMod, "alt|ctrl|winkey|alt+ctrl|alt+winkey|ctrl+winkey|alt+ctrl+winkey", $oldhotkeymod)
    GUICtrlSetData($TDS_SFKD_ComboKey, "{F1}|{F2}|{F3}|{F4}|{F6}|{F7}|{F8}|{F9}|{F10}|{F11}|{F12}|{SPACE}|{ENTER}|{BACKSPACE}|{DELETE}|{


SLOCK}|1|2|3|4|5|6|7|8|9|0|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|w|x|y|z", $oldhotkeykey)
    While 1
        $msgs = GUIGetMsg(1)
        $msg = $msgs[0]
        If $msgs[1] == $TDS_SFKD_GUI Then
                Case $msg = $GUI_EVENT_CLOSE
                    GUISetState(@SW_HIDE, $TDS_SFKD_GUI)
                Case $msg = $TDS_SFKD_button
                    $TDS_SFKD_RETURN_VALUE[0] = GUICtrlRead($TDS_SFKD_ComboMod)
                    $TDS_SFKD_RETURN_VALUE[1] = GUICtrlRead($TDS_SFKD_ComboKey)
                        Case $TDS_SFKD_RETURN_VALUE[0] == "ctrl"
                            $TDS_SFKD_RETURN_VALUE[2] = "^"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "alt"
                            $TDS_SFKD_RETURN_VALUE[2] = "!"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "winkey"
                            $TDS_SFKD_RETURN_VALUE[2] = "#"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "alt+ctrl"
                            $TDS_SFKD_RETURN_VALUE[2] = "!^"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "alt+winkey"
                            $TDS_SFKD_RETURN_VALUE[2] = "!#"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "ctrl+winkey"
                            $TDS_SFKD_RETURN_VALUE[2] = "^#"
                        Case $TDS_SFKD_RETURN_VALUE[0] == "alt+ctrl+winkey"
                            $TDS_SFKD_RETURN_VALUE[2] = "!^#"
                    Return $TDS_SFKD_RETURN_VALUE
                Case Else

There is a way to do what you have requested, but it requires too much code if you do it the "traditional" way

[well, i'm off to try doing it the "traditional" way!]


Edited by nfwu

Share this post

Link to post
Share on other sites

#include <GuiConstants.au3>
Func _GetHotKey()
Global $__G__OK = False
Local $keys = StringSplit( '|{F1}|{F2}|{F3}|{F4}|{F6}|{F7}|{F8}|{F9}|{F10}|{F11}|{F12}|{SPACE}|{ENTER}|{BACKSPACE}|{DELETE}|


PSLOCK}|1|2|3|4|5|6|7|8|9|0|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|w|x|y|z', '|')
For $j = 2 to $keys[0]
    HotKeySet('!'&$keys[$j], '_RespondToHotKey')
local $hotkeyget = GuiCreate('', 127, 64,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GuiCtrlCreateLabel('Please press a hotkey', 10, 10, 110, 20)
Local $Button_2 = GuiCtrlCreateButton("Cancel", 30, 30, 60, 20)

While $__G__OK == False
    $msg = GuiGetMsg()
    Case $msg = $GUI_EVENT_CLOSE
    Case $msg = $Button_2
For $j = 2 to $keys[0]
Return $__G__HOTKEY
Func _RespondToHotKey()
    $__G__OK = True
    $__G__HOTKEY = @HotKeyPressed

HOW TO USE: Call _GetHotKey. The HotKey will be the hotkey that is returned from this function.

KNOWN PROBLEM: only accepts hotkeys that are Alt+{SOMETHING} format. There is a limit to the number of hotkeys you can set, i think.


EDIT: fed up of trying to get rid of that &39; and &37;... they won;t go away no matter what i do!

Edited by nfwu

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...