2

I'm using autohotkey to swap Ctrl and Alt

LAlt::LCtrl
LCtrl::LAlt 

This works great, but on top of this i want to keep Alt+Tab and Ctrl+Tab where they originally were.

I've tried already a lot of different snippets of code, but none as really worked well so far.

The closest i've got to a fully working solution, but only for the Alt+Tab and no Shift+Alt+Tab is https://stackoverflow.com/questions/18454895/using-auto-hotkey-to-swap-ctrl-alt-and-implement-ctrl-tab

5 Answers 5

6

Got it, it works now!

It was in the right direction, but there were a couple of problems with the code. Especially the LShift was not being checked if it was false, so the first statement was always true.

I also added support for Ctrl+Tab.

*tab:: 
{   
    if (GetKeyState("LAlt", "P") AND GetKeyState("LShift", "P") = false) {     
        Send {LControl up}{LAlt down}{tab}
        KeyWait, tab  
    } else if (GetKeyState("LAlt", "P") AND GetKeyState("LShift", "P")) {     
        Send {LControl up}{LShift down}{LAlt down}{tab}
        KeyWait, tab
    } else if (GetKeyState("LCtrl", "P") AND GetKeyState("LShift", "P") = false) {     
        Send {LAlt up}{LCtrl down}{tab}
        KeyWait, tab
    } else if (GetKeyState("LCtrl", "P") AND GetKeyState("LShift", "P")) {  
        Send {LAlt up}{LShift down}{LCtrl down}{tab}
        KeyWait, tab
    } else if (GetKeyState("LWin", "P") AND GetKeyState("LShift", "P") = false) {     
        Send {LWin down}{tab}
        KeyWait, tab
    } else if (GetKeyState("LWin", "P") AND GetKeyState("LShift", "P")) {  
        Send {LShift down}{LWin down}{tab}
        KeyWait, tab
    } else {   
        send {tab}
    }      
    return
}

~LAlt Up::
{   
    send {LAlt up}
    return
}

~LCtrl Up::
{   
    send {LCtrl up}
    return
}

LAlt::LCtrl 
LCtrl::LAlt
1

As I'm working only from the example of the code provided in the answer you linked to, I put together the below code. I don't know what action "Shift+Alt+Tab" performs, as I get no response on my system with it, so you'll have to test for me to verify this has the desired effect.

*tab::
{   if (GetKeyState("LAlt", "P"))  
{   Send {LControl up}{Alt down}{tab}
    KeyWait, tab  
}else if (GetKeyState("LAlt", "P")) AND (GetKeyState("LShift", "P"))  
{ Send {LControl up}{LShift down}{Alt down}{tab}
    KeyWait, tab  
}else   
{   send {tab}
}      
return
}          
~LAlt Up::
{   send {lAlt up}
return
}
LAlt::LCtrl 
LCtrl::LAlt  
0
1

For me @herkulano version is not reliable on Windows10. Sometimes it runs different keys and I can't work.

Instead I use this which also works with Emacs for combinations like C-M--.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
; The .ahk text file needed to be saved with UTF8-BOM encoding rather than UTF8
; https://stackoverflow.com/questions/15635635/how-do-i-use-unicode-in-autohotkey
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

global sendLAltUpOnLCtrlUp := 0

;; https://superuser.com/questions/984343/remap-ctrl-to-alt-and-keep-alttab-and-ctrltab/1332713#1332713
;; https://autohotkey.com/board/topic/96465-switch-alt-and-ctrl-retain-alt-tab/
;; Let LCtrl sends LWin, LWin sends LAlt and LAlt sends LCtrl using SharpKeys and then
*tab:: 
{
    if (GetKeyState("LCtrl", "P") AND GetKeyState("LShift", "P") = false) {
        sendLAltUpOnLCtrlUp := 1
        Send {LCtrl up}{LAlt down}{tab}
        KeyWait, tab  
    } else
    if (GetKeyState("LCtrl", "P") AND GetKeyState("LShift", "P")) {
        sendLAltUpOnLCtrlUp := 1
        Send {LCtrl up}{LShift down}{LAlt down}{tab}
        KeyWait, tab
    } 
    else {   
        send {tab}
    }      
    return
}

~LCtrl up::
{   
    if(sendLAltUpOnLCtrlUp == 1) {
      sendLAltUpOnLCtrlUp := 0
      send {LAlt up}
    } else {
      send {LCtrl up}
    }
    return
}

~LAlt up::
{   
    send {LAlt up}
    return
}

;; Example how to insert polish diactrics with `RAlt + Shift + A` etc.
;; https://pl.m.wikipedia.org/wiki/Alfabet_polski
>!+a::Send {U+0104}
>!a::Send {U+0105}
0

I really liked @rofrol's version but it only handled sending Alt(+Shift)+Tab when Ctrl(+Shift)+Tab is pressed.

I also wanted the inverse, Ctrl(+Shift)+Tab when Alt(+Shift)+Tab is pressed.

Additionally it broke simple Shift+Tab functionality that I use all the time while coding, so I added an additional condition to fix that.

global sendLAltUpOnLCtrlUp := 0
global sendLCtrlUpOnLAltUp := 0

*tab::
{
  if (GetKeyState("LCtrl", "P") AND GetKeyState("LShift", "P"))
  {
    sendLAltUpOnLCtrlUp := 1
    Send {LCtrl up}{LShift down}{LAlt down}{tab}
    KeyWait, tab
  }
  else if (GetKeyState("LCtrl", "P"))
  {
    sendLAltUpOnLCtrlUp := 1
    Send {LCtrl up}{LAlt down}{tab}
    KeyWait, tab
  }
  else if (GetKeyState("LAlt", "P") AND GetKeyState("LShift", "P"))
  {
    sendLCtrlUpOnLAltUp := 1
    Send {LAlt up}{LShift down}{LCtrl down}{tab}
    KeyWait, tab
  }
  else if (GetKeyState("LAlt", "P"))
  {
    sendLCtrlUpOnLAltUp := 1
    Send {LAlt up}{LCtrl down}{tab}
    KeyWait, tab
  }
  else if (GetKeyState("LShift", "P"))
  {
    Send {LShift down}{tab}
    KeyWait, tab
  }
  else
  {
    send {tab}
  }
  return
}

~LCtrl up::
{
  if(sendLAltUpOnLCtrlUp == 1)
  {
    sendLAltUpOnLCtrlUp := 0
    send {LAlt up}
  }
  else
  {
    send {LCtrl up}
  }
  return
}

~LAlt up::
{
  if(sendLCtrlUpOnLAltUp == 1)
  {
    sendLCtrlUpOnLAltUp := 0
    send {LCtrl up}
  }
  else
  {
    send {LAlt up}
  }
  return
}
0

Put this in a separate .ahk :

SendMode Input 
*LAlt::
    send {LCtrl down}
    Keywait, Lalt
    send {LCtrl up}
return
*LCtrl::
    send {Lalt down}
    Keywait, LCtrl
    send {Lalt up}
return

And this in another .ahk:

SendMode Input 

LAlt & Tab::
    send {Lalt down}{Tab}
return 

+esc::Exitapp

Then run both scripts (run the first one first).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .