Mapping Shift & Alt to Ctrl

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
LazyMan
Posts: 26
Joined: 14 Jan 2014, 00:22
Location: peering out from behind my favorite rock

Mapping Shift & Alt to Ctrl

08 Feb 2014, 00:01

How can combination Shift & Alt be mapped to Ctrl?
It was originally asked at autohotkey.com: Two keys to one key
(It seemed trivial at first, but for my attempt at least, Shift stays down, as though I had used Send with {Blind}.)
If a working mapping does/can not exist, the explanation for why would be very welcome.
Thanks in advance. :)
RHCP
Posts: 202
Joined: 30 Sep 2013, 10:59

Re: Mapping Shift & Alt to Ctrl

08 Feb 2014, 12:15

I can't tell you the reason for this, but its due to Lshift being the first modifier and sending the Ctrl key.
The send command trips out AHK and it sees the shift key as being both logically AND PHYSICALLY up. That sort of hotkey forces the installation of the keyboard hook as well.

If the hotkey definition was the other way around, that is, LAlt & LShift, then after the send {Ctrl} AHK would see the alt key as physically and logically up.

This will work as you expect.

Code: Select all

LShift & LAlt:: Send {a}
LShift & LAlt Up:: Send {a Up}
I've noticed some other odd behaviours with hotkeys and modifiers before.


This hotkey may work as intended or close to it.

Code: Select all

#Persistent
#SingleInstance force
SetBatchLines, -1
#InstallMouseHook
#InstallKeybdHook
#UseHook On 

LAlt & LShift::
LShift & LAlt::
Send {BLIND}{Ctrl Down}{LShift UP}{LAlt UP}
; This must be an ||/or as getting the state 
; For LAlt & LShift::	Left shift is seen as physically down, but Left alt is UP!
; For LShift & LAlt::	Left alt is seen as physically down, but Left shift is UP!
; It is the send command of the modifiers above which causes this.
; Note although this is an OR you still need to hold both hotkeys down 
; if you intiated the hotkey via LAlt & LShift::
; I could probably change this behaviour, but I believe this is what you want anyway.

while GetKeyState("Shift", "P") || GetKeyState("Alt", "P")
{
	; ================================================================
	; Remove this tooltip - ITs just here to demonstrate the issue
	; ================================================================
	tooltip % A_Index 
			. "`n" GetKeyState("LShift", "P") " | " GetKeyState("LShift")
			. "`n" GetKeyState("LAlt", "P") " | " GetKeyState("LAlt")
	; ================================================================
	; Remove this tooltip - ITs just here to demonstrate the issue
	; ================================================================

	sleep 15
}
send {Ctrl Up}
; for increased security consider using  Send {BLIND}{LShift UP}{LAlt UP}{Ctrl UP} ; may not be required.
return 
Or even better using your method: Not only is it clearer, but the hotkey should end if you release either shift or alt.

Code: Select all

#Persistent
#SingleInstance force
SetBatchLines, -1
#InstallMouseHook
#InstallKeybdHook
#UseHook On 

LShift & LAlt:: Send {BLIND}{Ctrl Down}{LShift UP}{LAlt UP}
LShift & LAlt Up:: Send {BLIND}{Ctrl Up}

LAlt & LShift:: Send {BLIND}{Ctrl Down}{LShift UP}{LAlt UP}
LAlt & LShift Up:: Send {BLIND}{Ctrl Up}
User avatar
LazyMan
Posts: 26
Joined: 14 Jan 2014, 00:22
Location: peering out from behind my favorite rock

Re: Mapping Shift & Alt to Ctrl

09 Feb 2014, 22:54

Code: Select all

LShift & LAlt:: Send {Ctrl Down}
LShift & LAlt Up:: Send {Ctrl Up}
When using these hotkeys, KeyHistory reports that the Shift key is held down instead of released even though Send is used without {Blind}. Here is the corresponding KeyHistory output when using those hotkeys with Left:

Code: Select all

A0  02A	 	d	0.65	LShift
A4  038	h	d	0.14	LAlt
A4  038	i	u	0.00	LAlt
A2  01D	i	d	0.11	LControl
25  14B	 	d	0.25	Left
25  14B	 	u	0.05	Left
A0  02A	 	u	0.08	LShift
A4  038	h	u	0.00	LAlt
A2  01D	i	u	0.00	LControl
Can the intent of those hotkeys be achieved? If so, how?
Why, in this instance, is Shift not released?
RHCP
Posts: 202
Joined: 30 Sep 2013, 10:59

Re: Mapping Shift & Alt to Ctrl

10 Feb 2014, 00:04

Well the 'intent' of those hotkeys is achieved by both of those methods I posted. And they work for the user in the original thread.

Something about the sending of the {ctrl} key in conjunction with those two hotkey combinations (LAlt + LShift and LShift + LAlt) causes AHK to lose track of the state of the first modifier key.

Run the first hotkey I posted and take note of the logical and physical key states in the tooltip.
While both LShift and LAlt are being held down:
For LShift & LAlt:: Left alt is seen as physically down, but Left shift is PHYSICALLY and logically UP! (even though LShift is still physically down)
Since AHK sees the shift key as logically up, it doesn't bother to release the shift key during the send command. Even if it did, you probably still wouldn't achieve the result you're after, as you would effectively be sending {shift up}{ctrl down}{shift down}, since AHK will restore the modifier prefix as part of the send command. Hence you would be left with Shift and Ctrl down, rather than just Ctrl.
RHCP
Posts: 202
Joined: 30 Sep 2013, 10:59

Re: Mapping Shift & Alt to Ctrl

10 Feb 2014, 06:12

I posted a better solution in the linked thread.

Using a combination of sendEvent (to prevent the removal of the keyboard hook) and blind mode seems to work.
User avatar
LazyMan
Posts: 26
Joined: 14 Jan 2014, 00:22
Location: peering out from behind my favorite rock

Re: Mapping Shift & Alt to Ctrl

10 Feb 2014, 15:26

RHCP wrote:Using a combination of sendEvent (to prevent the removal of the keyboard hook) and blind mode seems to work.
Thank you for trying to help. :) But ...

The scripts' code below uses the default send mode--SendEvent. Adding the special key {Blind} to each Send does not change the key history at all; it is the same as without it! :o

In summary, pressing LShift+LAlt+Left with either

Code: Select all

LShift & LAlt:: Send {Control Down}
LShift & LAlt Up:: Send {Control Up}
or

Code: Select all

LShift & LAlt:: Send {Blind}{Control Down}
LShift & LAlt Up:: Send {Blind}{Control Up}
produces this key history:

Code: Select all

 	d	LShift
h	d	LAlt
i	u	LAlt
i	d	LControl
 	d	Left
 	u	Left
 	u	LShift
h	u	LAlt
i	u	LControl
I expected the first hotkeys to release both the Shift and Alt modifiers, since {Blind} is not specified, and I expected the second hotkeys to hold both the Shift and Alt modifiers down, since {Blind} is included. However, in both instances, Shift is held down and Alt is released. Is this unexpected behavior a bug/known issue for AHK or do I not understand AHK's documentation correctly?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu, Google [Bot], RussF and 196 guests