I want to start by saying I like what you guys have been doing so far. Like one of the posters on one of the first few pages, I also am a user of AutoCAD, and so I also frequently have to use the Capslock button for lots of capital letters. Since the script as presented doesn't actually work very well for users who make use of Capslock, I decided to rewrite the script.
Since this is my first real foray into AutoHotkey scripting, I decided to rewrite most of it so that I understand what's going on. I have removed parts of the script that I felt unnecessary (such as the part that disabled the normal right-click menu on the script and the german and french keyboard options). If you want or need them, feel free to copy those parts over from the current version (Edit 3, I believe) of the script.
So without further ado, I present my code:
Code:
#SingleInstance
ShiftDelay = 200
ShiftDelay *= -1
StringCaseSense, on
leftside := "``" . "12345qwertasdfgzxcvb"
rightside := "'" . "09876poiuy;lkjh/.,mn"
other := "-=[]\"
shiftleftside := "~" . "!@#$%QWERTASDFGZXCVB"
shiftrightside := """" . ")(*&^POIUY:LKJH?><MN"
shiftother := "_+{}|"
Loop % StrLen(leftside)
{
l := SubStr(leftside, A_Index, 1)
r := SubStr(rightside, A_Index, 1)
Hotkey Space & %l%, DoHotKey
Hotkey Space & %r%, DoHotKey
Hotkey Space & %l% UP, KeyUp
Hotkey Space & %r% UP, KeyUp
Hotkey %l%, NormalKeyDown
Hotkey %r%, NormalKeyDown
Hotkey %l% UP, KeyUp
Hotkey %r% UP, KeyUp
}
Loop % Strlen(other)
{
o := SubStr(other, A_Index, 1)
Hotkey %o%, normalKeyDown
Hotkey %o% UP, KeyUp
}
return
Control & Space::Suspend
Space & CapsLock::Send {Enter}
Space & Tab:: Send {Backspace}
+Space::Send {Space}
Space::Send {Space}
DoHotKey:
StringRight ThisKey, A_ThisHotkey, 1
MirrorKey := mirror(ThisKey)
Modifiers := ""
If (GetKeyState("LWin") || GetKeyState("RWin"))
Modifiers .= "#"
If (GetKeyState("Control"))
Modifiers .= "^"
If (GetKeyState("Alt"))
Modifiers .= "!"
If (GetKeyState("Shift"))
MirrorKey := shift(MirrorKey)
If (Modifiers <> "")
send %Modifiers%{%MirrorKey%}
Else
{
gosub, MaybePrintKey
akey := Mirrorkey
;akey is a global variable used to pass around the key that we care about
gosub, KeyDown
}
return
NormalKeyDown:
gosub, MaybePrintKey
akey := SubStr(A_ThisHotKey, 0, 1)
gosub, KeyDown
return
MaybePrintKey:
if(keysuccess <> True)
{
gosub, PrintKey
SetTimer, PrintShiftKey, off
}
return
KeyDown:
keysuccess := False
SetTimer, PrintShiftKey, %ShiftDelay%
return
KeyUp:
SetTimer, PrintShiftKey, off
gosub, PrintKey
return
PrintKey:
if(keysuccess <> True)
{
If (GetKeyState("CapsLock", "T"))
akey := togglecase(akey)
keysuccess := True
send {%akey%}
}
return
PrintShiftKey:
if(keysuccess <> True)
{
If (GetKeyState("CapsLock", "T"))
akey := togglecase(akey)
shiftkey := shift(akey)
send {%shiftkey%}
send {%akey% up}
keysuccess := True
}
return
togglecase(char)
{
StringUpper, u, char
StringLower, l, char
If (u == char)
char := l
Else
char := u
return char
}
mirror(char)
{
global leftside
global rightside
l := InStr(leftside, char)
r := InStr(rightside, char)
If (l > 0)
m := SubStr(rightside, l, 1)
Else If (r > 0)
m := SubStr(leftside, r, 1)
Else
m := char
return m
}
shift(char)
{
global leftside
global rightside
global other
global shiftleftside
global shiftrightside
global shiftother
normal := leftside . rightside . other
shifted := shiftleftside . shiftrightside . shiftother
n := InStr(normal, char, True)
s := InStr(shifted, char, True)
If(n > 0)
newchar := SubStr(shifted, n, 1)
Else If(s > 0)
newchar := SubStr(normal, s, 1)
Else
newchar := char
return %newchar%
}
A few ways in which it differs from edit 3:
- Caps Lock works as normal. It toggles the capitalization of letters that are typed, but does not interfere with other symbols.
- Like edit 3, it does shift characters when they are held down for a while (200ms is the default in this script; it is easily changed on line 3). However, it does not send the regular character, then a backspace, and then the shifted character as edit 3 does. It instead sends only the desired character. I find this advantageous, because backspace characters can screw some things up (like going to a previous page in old Internet Explorers and causing breaks in undo history).
- When you press and hold a character to capitalize it, you need to let go after it capitalizes, but before the key starts repeating. I am unable to find a reasonable solution to this problem. If anyone has knowledge of key auto-repeating, this script could use your expertise.
- It does not support keyboards other than standard US keyboards. This should be extensible by doing something similar to what is done in edit 3.[/code]