Need help for a new key from user input

Get help with using AutoHotkey and its commands and hotkeys
kwfine
Posts: 16
Joined: 26 Nov 2015, 06:46

Need help for a new key from user input

23 Jan 2018, 04:59

Hi all,

Recently, I wrote this script to help me remap my windows mouse left button, but some error happened.

Code: Select all

InputBox, UserInput, Testing,  `n`r(Choose a new key .), , 360, 200
MsgBox, You entered "%UserInput%"
UserInput::LButton
Can you help me please?

Kitty
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Need help for a new key from user input

23 Jan 2018, 08:47

As far as i known you can only use a variable with the hotkey command:

see : https://autohotkey.com/docs/commands/Hotkey.htm

Code: Select all

#Persistent 
InputBox, UserInput, Testing,  `n`r(Choose a new key .), , 360, 200
MsgBox, You entered "%UserInput%"

Hotkey, %UserInput%, RemapKey

RemapKey:
SendEvent {LButton}
return 

esc::exitapp
soundcloud.com/user-32706894
kwfine
Posts: 16
Joined: 26 Nov 2015, 06:46

Re: Need help for a new key from user input

24 Jan 2018, 04:22

noname wrote:As far as i known you can only use a variable with the hotkey command:

see : https://autohotkey.com/docs/commands/Hotkey.htm

Code: Select all

#Persistent 
InputBox, UserInput, Testing,  `n`r(Choose a new key .), , 360, 200
MsgBox, You entered "%UserInput%"

Hotkey, %UserInput%, RemapKey

RemapKey:
SendEvent {LButton}
return 

esc::exitapp
Thank you for the help!
Is RemapKey a custom function?
I have tried your suggested code with a little change in the code:

Code: Select all

#Persistent 
InputBox, UserInput, Testing,  `n`r(Choose a new key .), , 360, 200
MsgBox, You entered "%UserInput%"

Hotkey, %UserInput%, RemapKey

RemapKey(){
SendEvent {LButton}
}
return 

esc::exitapp
It works but I am not sure if this is correct.
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Need help for a new key from user input

24 Jan 2018, 05:48

If not a valid label name, this parameter can be the name of a function
I used Remapkey as a label , but as a function it looks better (but you need a recent version of ahk) :)
soundcloud.com/user-32706894
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Need help for a new key from user input

24 Jan 2018, 11:56

"Dynamic" (User-configurable) hotkeys are VERY HARD to do right, especially if you want to support all forms of input (Keyboard Keys, Mouse Buttons, Joystick Buttons, Joystick POV Hat directions). My solution (See below) took me hundreds of hours over a number years to perfect.

Quite likely, if you want to let end-users configure hotkeys, you also want to let them configure other settings via the GUI (Eg checkboxes, editboxes etc) and you will want these settings saved for you between runs of the script (Reads/writes settings to/from a text file)

I have written a library called AppFactory that does all this for you.
It allows you to easily add custom hotkey GuiControls that supports Keyboard/Mouse/Joystick, and also allows you to add special custom versions of the standard AHK GuiControls (Checkboxes, editboxes etc) that remember their values between runs.

In order to use it, you will need to learn how to use functions MyFunction() rather than Labels MyLabel:, and some other programming stuff, but learning that would be a hell of a lot simpler than learning how to write your own system that did what you want.

The thread is here: https://autohotkey.com/boards/viewtopic ... 1&p=177277
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Need help for a new key from user input

24 Jan 2018, 12:28

To clarify, here is your code snippet done with AppFactory.
The user can pick a hotkey, and whenever that hotkey is pressed or released, LButton gets pressed or released

Code: Select all

#SingleInstance force
#NoEnv
#Include AppFactory.ahk

; Initialize AppFactory
factory := new AppFactory()

; Add a custom Hotkey GuiControl called HK1, and set it to fire the InputEvent() function when it changes state
; The "w200" parameter is for size / position of the control, and is in the same format as when adding a normal AHK GuiControl
; ie this is basically saying the same thing as
; Gui, Add, Hotkey, w200
factory.AddInputButton("HK1", "w200", Func("InputEvent"))

Gui, Show
return

; This function gets called when the hotkey is pressed or released
; State holds 1 if the button was pressed, or 0 if it was released
InputEvent(state){
	Tooltip % "Button State: " state
	; Send output in response to the input
	if (state){
		Send {Lbutton down}
	} else {
		Send {Lbutton up}
	}
}

^Esc::
GuiClose:
	ExitApp
And here is me binding F12 to the hotkey and pressing it.
Image
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Need help for a new key from user input

24 Jan 2018, 12:43

And getting a little more sophisticated, this code shows how you can add a custom EditBox which holds the name of the key to send in response to the hotkey.

Code: Select all

#SingleInstance force
#NoEnv
#Include AppFactory.ahk

factory := new AppFactory()
factory.AddInputButton("HK1", "w200", Func("InputEvent"))
; Add a custom, peristent EditBox to hold the name of the key to send in response to the hotkey
; Set the default value to "LButton"
factory.AddControl("KeyToSend", "Edit", "xm w200", "LButton", 0)

Gui, Show, x0 y0
return

InputEvent(state){
	global factory
	; Get the name of the key to send from the KeyToSend EditBox
	keyToSend := factory.GuiControls.KeyToSend.Get()
	if (state){
		Send { %keyToSend% down}
	} else {
		Send { %keyToSend% up}
	}
}

^Esc::
GuiClose:
	ExitApp
Image

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 66 guests