How remap some LButton presses into MButton presses

Get help with using AutoHotkey and its commands and hotkeys
Randy31416
Posts: 46
Joined: 15 Jan 2014, 19:09

How remap some LButton presses into MButton presses

10 May 2015, 21:30

I've got a huge script that does all of my background keboard and mouse remapping work, and many other things as well. It works fine, and I'd like to keep it as intact as possible. But I've got a new machine that presents a special case.

The Surface Pro 3 includes a pen with two buttons on the side. Remapping of these keys is not supported by Microsoft, and they do not present themselves nicely as keystrokes that can be trapped. The pen, when tapping the screen with neither button pressed, presents LButton down and up. When the top button is pressed and the pen is tapped, it looks like RButton down and up. When the bottom button is pressed, though, the pen presents again LButton down and up. However, using the HID test functions at http://www.autohotkey.com/board/topic/3 ... functions/ I can see, in AutoHotkey, a WM_INPUT message that appears before the mouse down event is seen, and the data field of the WM_INPUT can disambiguate the tap-with-no-button case and the tap-with-bottom-button case. Thus, if I remember the state from the WM_INPUT message, I can decide to do different things when the LButton down and up events occur.

What I really want to do is to turn all of the LButton down and up events that occur with the bottom button pressed into MButton down and up events. So I am seeking a way to trap the LButton events early in the script and then remap them either into plain LButton events or into MButton events.

The difficulty is that I already trap LButton and MButton events, in many places, with many overrides (e.g. CTRL, WIN and so on) and under many IF and IFWINEXIST conditions. In a similar manner I have some level of InputLevel stuff appled in some cases. What I now seek is a way to remap these pen clicks before the rest of the script applies its logic to them, so that the rest of the script will see standard LButton and MButton events and continue to function as it does now with an ordinary mouse. So the mechanism I seek must trap the LButton, examine it, and then forward it to the rest of the script either as an LButton or as a MButton event for normal processing by the rest of the script.

Any thoughts or pointers?
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How remap some LButton presses into MButton presses

11 May 2015, 00:52

You can just use a normal *LButton hotkey with #if, but place it before the other hotkeys.

Code: Select all

; Hold CapsLock to simulate the "bottom button".
SetCapsLockState AlwaysOff
#if GetKeyState("CapsLock", "P")

*LButton:: ; This allows other modifiers, but has lower priority than ^LButton::.
; ^LButton:: ; Optional: This overrides the ^LButton hotkey below.
	Tooltip Pretending to hold MButton
	KeyWait LButton, P
	ToolTip ; Pretend to release MButton
	return

#IfWinActive ahk_class Progman  ; Desktop (for example)
LButton::MsgBox LButton with desktop active

#If ; Global
^LButton::MsgBox Ctrl & LButton
If you want the simulated MButton click to trigger other hotkeys in your script, use SendLevel.
Randy31416
Posts: 46
Joined: 15 Jan 2014, 19:09

Re: How remap some LButton presses into MButton presses

11 May 2015, 07:50

Consider this code (untested, though, but in response to the above).

Code: Select all

#If (1 = 1)
*LButton::
  SendLevel, 1
  SendInput, {MButton}
  Return
If I understand you and the documentation correctly, I think that should take LButton of any flavor and change it into a plain MButton and have any other plain MButton override anywhere below in the script react to it as if it were a plain MButton. Is there some shorthand way to do that SendInput so that the overrides are preserved (i.e. an external external Ctrl+Alt+LButton gets sent onward as a Ctrl+Alt+MButton)? If not, I can enumerate the prefix combinations under the #f. However, If I want to absolutely trigger all the cases that come up in the script below, such as ^!LButton, !+LButton, ^LButton, and the others, then, because the * prefix is lower in priority, I should do this.

Code: Select all

#If (1 = 1)
^!LButton::
  SendLevel, 1
  SendInput, ^!{MButton}
  Return
^LButton::
  SendLevel, 1
  SendInput, ^{MButton}
  Return
; and so on
Instead of (1 = 1) I would be doing the tests do disambiguate the lbutton presses sent by the pen and accepting only the ones I wanted to remap.
User avatar
Nextron
Posts: 1378
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How remap some LButton presses into MButton presses

11 May 2015, 09:19

It's funny that you're already talking about a remapping (which you need) but are using a hotkey.

Instead of *LButton::SendInput, {MButton} ;and some other commands you could use:

Code: Select all

#InputLevel 1
LButton::MButton
#InputLevel 0
This is a remapping with the increased SendLevel. The only thing missing is a change to SendInput. If you actually need that, you could set it to default globally using SendMode Input in your auto-executing section, or locally by manually setting up the remapping as described here but instead use SendInput.
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How remap some LButton presses into MButton presses

11 May 2015, 16:24

Randy31416 wrote:Is there some shorthand way to do that SendInput so that the overrides are preserved
SendInput {Blind}{MButton} will leave the modifier keys as they are. So will Click Middle. You still need to list a hotkey for each one if you want to override other non-* hotkeys, but you can stack the labels like so:

Code: Select all

#If true
^!LButton::
^LButton::
;etc.
   SendLevel 1
   Click Middle
   return
@Nextron: The remapping will not override other hotkeys in the script, such as ^LButton. Randy would need to write out the equivalent hotkeys in full and add multiple hotkey labels (like above); but this may be unnecessary.
Randy31416
Posts: 46
Joined: 15 Jan 2014, 19:09

Re: How remap some LButton presses into MButton presses

11 May 2015, 19:02

lexikos wrote:SendInput {Blind}{MButton} will leave the modifier keys as they are. So will Click Middle

Code: Select all

#If true
^!LButton::
^LButton::
;etc.
   SendLevel 1
   Click Middle
   return
This works very nicely and is exactly what I was hoping for. I do have to stack all eight combos of CTRL, ALT, SHIFT because of subsequent overrides, but, with this little block of code and the disambiguation function that is used in the #If, the rest of the script (pushing 10000 lines) needs no changes. Thanks very much all of you for the help.
User avatar
Nextron
Posts: 1378
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How remap some LButton presses into MButton presses

12 May 2015, 03:06

lexikos wrote:@Nextron: The remapping will not override other hotkeys in the script, such as ^LButton. Randy would need to write out the equivalent hotkeys in full and add multiple hotkey labels (like above); but this may be unnecessary.
:oops: Time for me to update. It does work like this in 1.1.14.
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How remap some LButton presses into MButton presses

12 May 2015, 03:40

If you're saying that what I said doesn't apply to v1.1.14, I'm pretty sure you're mistaken. ^LButton:: takes precedence over *LButton:: regardless of #if (provided they're both active) or order in the script. I don't have v1.1.14.00 on hand, but I've tested and confirmed v1.1.13.00, v1.1.14.01, v1.1.22.00 and even v1.0.48.05 behave as I said.

Of course, ^LButton will not fire if you are not holding the Ctrl key or if you are holding other modifiers as well.
User avatar
Nextron
Posts: 1378
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How remap some LButton presses into MButton presses

12 May 2015, 04:47

Ok, done some testing too using keyboard buttons instead of mouse buttons (didn't want to loose control of my mouse buttons) which explains my aberrant observation, which is indeed version independent.

When using the hook the most specific hotkey takes precedence, as lexikos explained, which is always the case for mouse hotkeys. My keyboard test script used registered hotkeys and a hook remap which gives the remap precedence over registered hotkeys. However, sometimes my registered hotkeys would load as hook hotkeys, changing the priority, which lead me to the mistaken conclusion it was a version dependency. That last oddity is something I can't reliably reproduce, but perhaps it was the script editor being slow to save a modification to the test script.

My apologies for the confusion.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, mikeyww, TAC109, VxMiMxV and 43 guests