Key & any key?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Scr1pter
Posts: 492
Joined: 06 Aug 2017, 08:21
Location: Germany

Key & any key?

12 Jul 2018, 16:15

Hello guys,

I would like to realize this:
If I press the < key, the output should be left mouse button down.
When releasing the < key, the output should be left mouse button up.
(The key is next to left shift on a German keyboard.)

This works:

Code: Select all

$<::
Send {lbutton down}
KeyWait, <
Send {lbutton up}
return
Now I would also like to include the functionality that if < is pressed with any key at the same time,
the output should be Lctrl+Any key.

When I use this code for example, it works, but
1st:
I would need to create like 50 other combinations
Time-consuming and probably not needed.

2nd:
The left mouse button down doesn't work anymore.
When pressing < the output is left mouse button, but when holding <, left mouse button isn't being held anymore.

Code: Select all

$<::
Send {lbutton down}
KeyWait, <
Send {lbutton up}
return

^<::
Send <
return

< & c::
Send ^c
return
Pressing <+c is like Lctrl+C, but as said, there are those 2 issues.
I also tried it this way, but it says "Invalid hotkey" when trying to run the script:

Code: Select all

$<::
Send {lbutton down}
KeyWait, <
Send {lbutton up}
return

^<::
Send <
return

< & A_ThisHotkey::
Send ^A_ThisHotkey
return
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters!
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit - AHK version: 1.1.29.00
Rohwedder
Posts: 1435
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Key & any key?

12 Jul 2018, 23:08

Hallo,
try:

Code: Select all

<::LButton
User avatar
Scr1pter
Posts: 492
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Key & any key?

13 Jul 2018, 05:55

Hallo,
Unfortunately, it's the same.

And what about the A_ThisHotkey issue?
Any idea?

Thanks anyway!
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters!
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit - AHK version: 1.1.29.00
Rohwedder
Posts: 1435
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Key & any key?

13 Jul 2018, 07:05

Sorry, I hadn't read your request very carefully.
You have to choose! Either "<" becomes "LButton":

Code: Select all

<::LButton
or "LCtrl":

Code: Select all

<::LCtrl
Both at the same time and context, doesn't work!
GEV
Posts: 690
Joined: 25 Feb 2014, 00:50

Re: Key & any key?

13 Jul 2018, 07:35

Code: Select all

Keys := ["a","b","c","d","e","f","g","h"] ; ...
for each, key in Keys
	Hotkey, < & %key%, Ctrl_Key, On
return

<::LButton
^<::Send <

Ctrl_Key:
	Send {Lbutton up}{Ctrl Down}%A_ThisHotkey%
	KeyWait, <
	Send {Ctrl Up}{Lbutton up}
return
downside:
"< & %key%" leaves in Notepad++ an RS mark.
swagfag
Posts: 1375
Joined: 11 Jan 2017, 17:59

Re: Key & any key?

13 Jul 2018, 07:48

Code: Select all

#NoEnv
#SingleInstance Force
SendMode Input
SetBatchLines -1

pressed := false ; we introduce a layer of hotkeys which will become active while '>' is being held down
GenerateCombinationHotkeys()
Return

#If !pressed
*VKBE:: ; '>'
	pressed := true
	Send {Blind}{LButton Down}
Return
#If

*VKBE Up:: ; '>'
	pressed := false
	Send {Blind}{LButton Up}
Return

#If pressed
VKBE::return ; this guards against keyboard repeat kicking in, while '>' is being held down. on my mechanical at least that is
#If

Esc::ExitApp

GenerateCombinationHotkeys() {
	fn := Func("CtrlAnyKey")
	Hotkey If, % "pressed"
	Loop 254 ; see https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
	{
		if (A_Index = 190) ; skip defining '>' for now
			continue

		Hotkey % Format("*VK{:X}", A_Index), % fn
	}
	Hotkey If
}

CtrlAnyKey() {
	Send % "{Blind}{Ctrl Down}{" StrReplace(A_ThisHotkey, "*") "}{Ctrl Up}"
}
lexikos
Posts: 6203
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Key & any key?

14 Jul 2018, 01:47

If the requirements stated in the OP are met, < will click LButton even when you intend to use it as Ctrl. There may be trouble if you aren't conscious of the mouse position when you use the hotkey. That aside, there are methods simpler than those shown above.

The first method puts LCtrl down unconditionally to produce "Lctrl+Any key", but may have side-effects when "<" is used as LButton. For instance, it causes Explorer drag-drop to copy instead of move. Commands are ordered to avoid producing a Ctrl+click.

Code: Select all

*<::
    if !GetKeyState("LButton") {
        SetMouseDelay -1
        Click Left Down
    }
    if !GetKeyState("Ctrl") {  ; This could be implied by LButton state.
        SetKeyDelay -1
        Send {Blind}{Ctrl DownR}  ; DownR requires AutoHotkey v1.1.27+
    }
    return

*< up::
    SetKeyDelay -1
    Send {Blind}{Ctrl up}
    SetMouseDelay -1
    Click Left Up
    return
On the US layout, that would require Shift+,. A small modification would register whichever key produces "<" but without any modifiers:

Code: Select all

lb_ctrl_key := GetKeyName("<")  ; Automatically use "," on US layout.
Hotkey *%lb_ctrl_key%, lb_ctrl_down
Hotkey *%lb_ctrl_key% up, lb_ctrl_up
return

lb_ctrl_down:
    if !GetKeyState("LButton") {
        SetMouseDelay -1
        Click Left Down
    }
    if !GetKeyState("Ctrl") {  ; This could be implied by LButton state.
        SetKeyDelay -1
        Send {Blind}{Ctrl DownR}  ; DownR requires AutoHotkey v1.1.27+
    }
    return

lb_ctrl_up:
    SetKeyDelay -1
    Send {Blind}{Ctrl up}
    SetMouseDelay -1
    Click Left Up
    return
If you need to avoid combining LButton and Ctrl, you can change or add requirements. For example:
  1. Do not click down when < is pressed; instead, click down and up if and when < is released without having combined it with another key. For example:

    Code: Select all

    lb_ctrl_key := GetKeyName("<")  ; Automatically use "," on US layout.
    Hotkey *%lb_ctrl_key%, lb_ctrl_down
    Hotkey *%lb_ctrl_key% up, lb_ctrl_up
    return
    
    lb_ctrl_down:
        SetKeyDelay -1
        Send {Blind}{Ctrl DownR}  ; DownR requires AutoHotkey v1.1.27+
        return
    
    lb_ctrl_up:
        SetKeyDelay -1
        Send {Blind}{Ctrl up}
        if (A_PriorKey = lb_ctrl_key) {
            SetMouseDelay -1
            Click Left
        }
        return
  2. Do not send {Ctrl down} when < is pressed; instead, send it immediately before the first combination. There are at least a few ways to achieve this:
    • Use dynamic hotkeys as shown by GEV and swagfag. Let the hotkey block the key-press so that it can be sent after {Ctrl down}.
    • Use the Input command to wait for the first combination. See the SingleKey example. This requires a long list of keys, and might be less reliable due to timing issues. However, it does avoid registering hundreds of hotkeys (there is currently a limit of 1000 unique hotkeys). It also avoids conflicts with existing hotkeys.
    • Register your own keyboard hook with DllCall (search "WH_KEYBOARD_LL"). A keyboard hook can intercept the next keypress and inject {Ctrl down} without having to block and resend.
  3. To preserve drag-drop, click down when the mouse begins moving, and click up when < is released. There are at least two ways to achieve this:
    • Use MouseGetPos to get the initial position and to poll for changes (in combination with SetTimer). Once movement is detected, move the mouse back to the original position, click down and "replay" the initial movement. This is simplest but not the most reliable.
    • Register your own mouse hook with DllCall (search "WH_MOUSE_LL") to intercept mouse movement and inject LButton-down before the cursor position actually changes. This is complicated. To ensure there is no impact on mouse performance, the mouse hook can be installed when < is pressed and uninstalled when < is released.
GEV's method requires each key to be in the list.

swagfag's method only works for keys which are handled by VK code, so it fails for the following: NumpadEnter, Delete, Insert, Up, Down, Left, Right, Home, End, PgUp, PgDn. It may also fail for other keys if corresponding hotkeys are defined by scan code.

Return to “Ask For Help”

Who is online

Users browsing this forum: Chunjee, Flipeador, Google [Bot], hymal7, lluke, m33mt33n and 81 guests