Key & any key?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Scr1pter
Posts: 370
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] [Download] GeSHi © Codebox Plus

$<::
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] [Expand] [Download] GeSHi © Codebox Plus



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:

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

Re: Key & any key?

12 Jul 2018, 23:08

Hallo,
try:
User avatar
Scr1pter
Posts: 370
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! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
Rohwedder
Posts: 1277
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":
or "LCtrl":
Both at the same time and context, doesn't work!
GEV
Posts: 689
Joined: 25 Feb 2014, 00:50

Re: Key & any key?

13 Jul 2018, 07:35

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



downside:
"< & %key%" leaves in Notepad++ an RS mark.
swagfag
Posts: 1080
Joined: 11 Jan 2017, 17:59

Re: Key & any key?

13 Jul 2018, 07:48

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

lexikos
Posts: 6129
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] [Expand] [Download] GeSHi © Codebox Plus


On the US layout, that would require Shift+,. A small modification would register whichever key produces "<" but without any modifiers:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


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] [Expand] [Download] GeSHi © Codebox Plus

  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: lluke, logarithm, sn0365 and 87 guests