Trigger Relay on keyboard press down and up Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
azzajess
Posts: 4
Joined: 30 Jan 2023, 06:16
Contact:

Trigger Relay on keyboard press down and up

Post by azzajess » 30 Jan 2023, 06:39

Hey all,
My objective is to create a script that opens a relay on key press down, and closes on key up. Works on all keyboard keys and not mouse. The idea is the relay is like a keyboard solenoid/clicker that opens and closes based on key down and up.

I modified a script based off Rohwedder which works mostly but cant differentiate between key press down or up so i can open or close the relay separately.

Any idea what the best way to do this would be? I still want the keyboard to continue doing keyboard things but just simply open a relay and keypress down and close relay on keypress release. Is there a modification of this code that can be done or does it need to be done from scratch?

The code simply sends the bytes using SerialSend.exe to activate the LCUS-1 USB relay.

Code: Select all

;code from Rohwedder (https://www.autohotkey.com/boards/viewtopic.php?style=17&f=76&t=77075) 	
Gui, +HwndGui
Gui, Add, Edit, vMyEdit
Gui, Show
Format := "SC{:X}"
Loop, 0x1FF
	Gosub, TestEndkey
Format := "VK{:X}"
Loop, 0xFF
	Gosub, TestEndKey
Gui, Destroy
Loop
{
	ih := InputHook("L1 M V", EndKeys)
	ih.Start(), ih.Wait()
	;Runs software SerialSend to communicate with LCUS-1 over com port 3 with baudrate of 9600.
	;relay on
	run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x01\xA2",,Hide
	;delay
	sleep, 10
	;relay off
	run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x00\xA1",,Hide
}
Return
TestEndKey:
IF ("" = Key := GetKeyName(Format(Format, A_Index)))
	Or InStr(Key, "Button") Or InStr(Key, "Wheel")
	Return
ControlSend, Edit1,% Key := "{" Key "}", ahk_id %Gui%
Gui, Submit
IF (MyEdit = "") And !Instr(EndKeys, Key)
	EndKeys .= Key
ControlSend, Edit1, {Bs}, ahk_id %Gui%
Return
Any help would be greatly appreciated.

Let me know if you have any more questions.

User avatar
mikeyww
Posts: 26945
Joined: 09 Sep 2014, 18:38

Re: Trigger Relay on keyboard press down and up

Post by mikeyww » 30 Jan 2023, 07:18

Welcome to this AutoHotkey forum!

Some ideas are below.

Code: Select all

#Requires AutoHotkey v1.1.33
Loop 96 {
 hk := "~" GetKeyName(Chr(A_Index + 31))
 Hotkey % hk      , RelayOn
 Hotkey % hk " Up", RelayOff
}
Return

RelayOn:
hk := SubStr(A_ThisHotkey, 2)
SoundBeep 1500 ; Relay commands here
KeyWait % hk
Return

RelayOff:
SoundBeep 1000
Return
If you are new to AutoHotkey, I would use version 2.

Code: Select all

#Requires AutoHotkey v2.0
Loop 96 {
 hk := "~" GetKeyName(Chr(A_Index + 31))
 Hotkey hk      , relayOn
 Hotkey hk " Up", relayOff
}

relayOn(ThisHotkey) {
 SoundBeep 1500                ; Open relay
 KeyWait SubStr(ThisHotkey, 2)
}

relayOff(ThisHotkey) {
 SoundBeep 1000                ; Close relay
}

azzajess
Posts: 4
Joined: 30 Jan 2023, 06:16
Contact:

Re: Trigger Relay on keyboard press down and up

Post by azzajess » 31 Jan 2023, 04:31

Thanks so much for that, its pretty much what i have been looking for.

With this method looks like only modifier keys arent working (Shift, ctrl, alt, caps, tab, function keys, arrows keys etc), so I used this script (from robertcollier4 that was extracted from KeyPressOSD (https://autohotkey.com/boards/viewtopic.php?t=225) and implemented it here. It works but is is this the best way to do something like this or is there a more efficient way?

Code: Select all

#Requires AutoHotkey v1.1.33
Loop 96 {
 hk := "~" GetKeyName(Chr(A_Index + 31))
 Hotkey % hk      , RelayOn
 Hotkey % hk " Up", RelayOff
}
Loop, 24 ; F1-F24
	{
		Hotkey, % "~*F" A_Index, relayOn
		Hotkey, % "~*F" A_Index " Up", relayOff
	}

Loop, 10 ; Numpad0 - Numpad9
	{
		Hotkey, % "~*Numpad" A_Index - 1, relayOn
		Hotkey, % "~*Numpad" A_Index - 1 " Up", relayOff
	}

Otherkeys := "XButton1|XButton2|Browser_Forward|Browser_Back|Browser_Refresh|Browser_Stop|Browser_Search|Browser_Favorites|Browser_Home|Volume_Mute|Volume_Down|Volume_Up|Media_Next|Media_Prev|Media_Stop|Media_Play_Pause|Launch_Mail|Launch_Media|Launch_App1|Launch_App2|Help|Sleep|PrintScreen|CtrlBreak|Break|AppsKey|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|Tab|Enter|Esc|BackSpace"
	           . "|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause|sc145|sc146|sc046|sc123|Ctrl|Alt|Shift"
Loop, parse, Otherkeys, |
	{
		Hotkey, % "~*" A_LoopField, relayOn
		Hotkey, % "~*" A_LoopField " Up", relayOff
	}
Return

RelayOn:
hk := SubStr(A_ThisHotkey, 2)
;SoundBeep 1500 ; Relay commands here
run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x01\xA2",,Hide
KeyWait % hk
Return

RelayOff:
;SoundBeep 1000
run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x00\xA1",,Hide
Return
Ill definitely try an ahk2 version as well later

Thanks again! Its definitely closer than I ever was :)

azzajess
Posts: 4
Joined: 30 Jan 2023, 06:16
Contact:

Re: Trigger Relay on keyboard press down and up

Post by azzajess » 31 Jan 2023, 04:38

Thanks so much for the replay mikeyww, thats pretty much what I was looking for!
(Apologies if this is sent twice, not sure what happened the first time?)

When trying this script, i noticed that it didnt include the modifier keys, function keys, arrow keys etc. So I grabbed some info/code from this thread (https://www.autohotkey.com/boards/viewtopic.php?t=34014) down the bottom that robertcollier4 posted and was wondering if that's the best way to do it or is there a more efficient way?

Code: Select all

#Requires AutoHotkey v1.1.33
Loop 96 {
 hk := "~" GetKeyName(Chr(A_Index + 31))
 Hotkey % hk      , RelayOn
 Hotkey % hk " Up", RelayOff
}
Loop, 24 ; F1-F24
	{
		Hotkey, % "~*F" A_Index, relayOn
		Hotkey, % "~*F" A_Index " Up", relayOff
	}

Loop, 10 ; Numpad0 - Numpad9
	{
		Hotkey, % "~*Numpad" A_Index - 1, relayOn
		Hotkey, % "~*Numpad" A_Index - 1 " Up", relayOff
	}

Otherkeys := "XButton1|XButton2|Browser_Forward|Browser_Back|Browser_Refresh|Browser_Stop|Browser_Search|Browser_Favorites|Browser_Home|Volume_Mute|Volume_Down|Volume_Up|Media_Next|Media_Prev|Media_Stop|Media_Play_Pause|Launch_Mail|Launch_Media|Launch_App1|Launch_App2|Help|Sleep|PrintScreen|CtrlBreak|Break|AppsKey|NumpadDot|NumpadDiv|NumpadMult|NumpadAdd|NumpadSub|NumpadEnter|Tab|Enter|Esc|BackSpace"
	           . "|Del|Insert|Home|End|PgUp|PgDn|Up|Down|Left|Right|ScrollLock|CapsLock|NumLock|Pause|sc145|sc146|sc046|sc123|Ctrl|Alt|Shift"
Loop, parse, Otherkeys, |
	{
		Hotkey, % "~*" A_LoopField, relayOn
		Hotkey, % "~*" A_LoopField " Up", relayOff
	}
Return

RelayOn:
hk := SubStr(A_ThisHotkey, 2)
;SoundBeep 1500 ; Relay commands here
run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x01\xA2",,Hide
KeyWait % hk
Return

RelayOff:
;SoundBeep 1000
run,SerialSend /devnum 9 /baudrate 9600 /hex "\xA0\x01\x00\xA1",,Hide
Return
Ill try and implement a ahk v2 version when I get the chance.

Thanks so much!

gregster
Posts: 9022
Joined: 30 Sep 2013, 06:48

Re: Trigger Relay on keyboard press down and up

Post by gregster » 31 Jan 2023, 04:45

azzajess wrote:
31 Jan 2023, 04:38
(Apologies if this is sent twice, not sure what happened the first time?)
As a very new user, your posts need to be approved by a moderator.
Please be patient, this can take some time sometimes; we are a volunteer community. Thank you!


azzajess
Posts: 4
Joined: 30 Jan 2023, 06:16
Contact:

Re: Trigger Relay on keyboard press down and up

Post by azzajess » 31 Jan 2023, 21:11

Thanks for the help! I'll definitely play around with this.

Post Reply

Return to “Ask for Help (v1)”