0

I have a AutoHotKey script that asks me if I want to remap my Win keys to Ctrl or cancel their remapping, thus making them Win keys again.

However I cannot find a way to cancel the remap. If I use the command LWin::Lwin I get the error message that there is a "duplicate key".

I'm new to AutoHotKey, but I did search first, so please don't bite my head off is this is a stupid question. (It is a Lenovo laptop with Windows7-64).

Here's the script:

MsgBox, 4, , Remap CTRL for Desktop Keyboard?
IfMsgBox, Yes
   LWin::LCtrl
   RWin::RCtrl
   return
; Otherwise, the user picked No
;   LWin::LWin
;   RWin::RWin
;   return
emecas
  • 1,586
  • 3
  • 27
  • 43
user2262825
  • 1
  • 1
  • 1

2 Answers2

1

Various ways.

Create a hotkey to close ahk, e.g. ^!x::ExitApp = [Ctrl]+[Alt]+[x]

Create a hotkey to disable/enable all hotkeys e.g. f12::suspend

Create hotkeys that ONLY work in a specific appliaction.

Here are all suggestions combined. Under normal circumstances: LWin::LCtrl and RWin::RCtrl are active, Unless you pressed F12. You can in AHK_L set variables that can be used in #If (Var = 1), where you can define Hotkeys that only work when that variable is set to 1 (true).

SetTitleMatchMode, 2 ; Allow the use of a portion of the wintitle
F12::
Suspend
If A_IsSuspended
    TrayTip, HotKeys, Off, 3, 0
Else
    TrayTip, HotKeys, On, 3, 0
Return

^!x::ExitApp
LWin::LCtrl
RWin::RCtrl
F1::MsgBox, Normal Mode

#IfWinActive, Window title
  F1::MsgBox, Window X is active
  F2::MsgBox, You pressed F2 inside Window x
#IfWinActive

Toggle := False
F10::Toggle := !Toggle ; Turns Mouse button ON|Off
#if Toggle ; ONLY worls in AHK_L
     LButton::Return ; Disables Mouse button
#if
Lexikos
  • 987
  • 6
  • 16
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • If your script is run verbatim, it does not initialize *Toggle* - the `Toggle := False` line is never executed. That's okay in this case, since `Toggle := !Toggle` will set *Toggle* to true if it was uninitialized (empty). But it does seem to perpetuate a misunderstanding. Also, `SetTitleMode` is invalid, your F12 hotkey shows "hotkeys on" when the hotkeys are really off and can't unsuspend since the F12 hotkey itself is suspended (as Suspend is not the hotkey's first line). – Lexikos Mar 02 '15 at 11:23
  • Apparently I can edit answers, so I've corrected the worst errors. Also added `\`code\`` marks. – Lexikos Mar 02 '15 at 11:27
  • @Lexikos, Thank you for the corrections and additions. – Robert Ilbrink Mar 03 '15 at 09:37
-1

Here's a version you can drive from the command line:

; Allow the script to be reloaded multiple times
#SingleInstance force

; Check the command line for input
NumberOfParameters = %0%

; If any command line param was passed then just unload the mappings
If ( NumberOfParameters > 0 )
{
  MsgBox Command line parameter was passed, unloading...
  ExitApp
}
Else
{
  ; Let's ask the user what they want to do
  MsgBox, 4, , Remap CTRL for Desktop Keyboard?

  IfMsgBox, Yes
  {
    ; If yes, then remap
    MsgBox Keys have been mapped.
  }
  Else
  {
    ; If no, then unload
    MsgBox Unloading mapping.
    ExitApp
  }
}

; Keys will be mapped so long as the script remains resident
LWin::LCtrl
RWin::RCtrl
Chris
  • 14
  • 2
  • You cannot conditionally remap a key that way. The remappings `LWin::LCtrl` and `RWin::RCtrl` will be active immediately upon starting the script, regardless of the result of the MsgBox or even whether the MsgBox is shown at all. – Lexikos Mar 02 '15 at 11:17
  • I adjusted the positioning of the mappings to better reflect the true logic. So long as the script is resident, the mappings will remain in place. – Chris Mar 05 '16 at 00:05