-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigCtrl.ahk
195 lines (178 loc) · 6.59 KB
/
BigCtrl.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
; Copyright (c) 2012 Benjamin Hansen
;
; Permission is hereby granted, free of charge, to any person
; obtaining a copy of this software and associated documentation files
; (the "Software"), to deal in the Software without restriction,
; including without limitation the rights to use, copy, modify, merge,
; publish, distribute, sublicense, and/or sell copies of the Software,
; and to permit persons to whom the Software is furnished to do so,
; subject to the following conditions:
;
; The above copyright notice and this permission notice shall be
; included in all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;
; Allows the spacebar key to mimic the Ctrl key while retaining most
; of its normal functionality. Holding down the spacebar key down acts
; like holding down the ctrl key. This allows for easier use of
; keyboard shortcuts (such as Ctrl+C for copy). If the spacebar key is
; pressed and released quickly (less than the specified timeout) and
; no other key was pressed then a normal space is sent.
;
; Author: Ben Hansen <benhansenslc@gmail.com>
;+Capslock::Capslock
;Capslock::Ctrl
#SingleInstance force
#NoEnv
SendMode Input
SetStoreCapslockMode, Off
Process, priority, , High
; The amount of milliseconds of holding the spacebar after which a
; space key is no longer returned.
g_TimeOut := 300
; The amount of milliseconds to delay returning a Ctrl key sequence
; that are potentially accidentally hit with the space bar. If the
; space bar comes up during this delay the regular keys will be
; returned instead. Probably rounds to the nearest 10 milliseconds by
; the OS.
g_Delay := 70
g_SpacePressDownTime := false
g_OtherKeyPressed := false
g_SkipNextSpace := false
allKeysStr := "LButton*RButton*MButton*WheelDown*WheelUp*WheelLeft*WheelRight*XButton1*XButton2*Tab*Enter*Escape*Backspace*Delete*Insert*Home*End*PgUp*PgDn*Up*Down*Left*Right*ScrollLock*CapsLock*NumLock*Numpad0*Numpad1*Numpad2*Numpad3*Numpad4*Numpad5*Numpad6*Numpad7*Numpad8*Numpad9*NumpadDot*NumpadDiv*NumpadMult*NumpadAdd*NumpadSub*NumpadEnter*F1*F2*F3*F4*F5*F6*F7*F8*F9*F10*F11*F12*F13*F14*F15*F16*F17*F18*F19*F20*F21*F22*F23*F24*AppsKey*Browser_Back*Browser_Forward*Browser_Refresh*Browser_Stop*Browser_Search*Browser_Favorites*Browser_Home*Volume_Mute*Volume_Down*Volume_Up*Media_Next*Media_Prev*Media_Stop*Media_Play_Pause*Launch_Mail*Launch_Media*Launch_App1*Launch_App2*Help*Sleep*PrintScreen*CtrlBreak*Pause*Break"
StringSplit, allKeysArray, allKeysStr, *
Loop %allKeysArray0%
{
key := allKeysArray%A_Index%
Hotkey, % "~*"key, ListenForKey
}
; Keys that are possible to accidentally press with the space key
; while typing fast.
keysToDelayStr := "1*2*3*4*5*6*7*8*9*0*q*w*e*r*t*y*u*i*o*p*[*]*\*a*s*d*f*g*h*j*k*l*;*'*z*x*c*v*b*n*m*,*.*/"
StringSplit, keysToDelayArray, keysToDelayStr, *
Loop %keysToDelayArray0%
{
key := keysToDelayArray%A_Index%
Hotkey, % "*"key, DelayKeyOutput
}
; This is necessary to prevent wierd bugs from occuring with the other
; modifier keys.
modifiersStr := "LWin*RWin*LAlt*RAlt*LShift*RShift"
StringSplit, modifiersArray, ModifiersStr, *
Loop %modifiersArray0%
{
key := modifiersArray%A_Index%
Hotkey, % "*"key, ModifierDown
Hotkey, % "*"key " up", ModifierUp
}
ListenForKey:
g_OtherKeyPressed := true
Return
DelayKeyOutput:
Critical
pressedKey := SubStr(A_ThisHotkey,0)
modifiers := GetModifiers()
; Only wait to see if the space comes up if 1) the space bar key is
; down in the first place and 2) it has been held down for less than
; the timeout and 3) another Ctrl key combo hasn't already been
; pressed.
if((g_SpacePressDownTime != false)
&& (GetSpaceBarHoldTime() < g_TimeOut) && !g_OtherKeyPressed)
{
; Do the sleeping of timeout in small increments, that way if the
; the space key is released in the middle we can quit early.
wait_start_time := A_TickCount
while A_TickCount - wait_start_time + 10 < g_Delay
{
Sleep, 10
if(!getKeyState("Space", "P"))
{
; Since space bar was released, remove the Ctrl modifier.
StringReplace, modifiers, modifiers, ^,
; Force space to fire, because its being released could not
; fire during this routine because this thread is critical.
Gosub *Space up
; Stop the space in the event queue from firing since we
; have already fired it manually.
g_SkipNextSpace := True
Break
}
}
}
SendInput % modifiers pressedKey
g_OtherKeyPressed := true
Return
ModifierDown:
pressedKey := SubStr(A_ThisHotkey,2) ; Get all put starting '*'
SendInput {%pressedKey% down}
Return
ModifierUp:
pressedKey := SubStr(A_ThisHotkey,2) ; Get all put starting '*'
SendInput {%pressedKey%}
Return
*Space::
Critical
; Don't update on OS simulated repeats but only when the user
; actually pressed the key down for the first time
if(g_SpacePressDownTime == false)
{
g_SpacePressDownTime := A_TickCount
g_OtherKeyPressed := false
}
SendInput {RCtrl down}
Return
*Space up::
Critical
if(g_SkipNextSpace)
{
g_SkipNextSpace := false
}
SendInput {RCtrl up}
if(g_OtherKeyPressed == true)
{
g_SpacePressDownTime := false
Return
}
if (GetSpaceBarHoldTime() <= g_TimeOut)
{
modifiers := GetModifiers()
SendInput % modifiers "{Space}"
}
g_SpacePressDownTime := false
Return
GetSpaceBarHoldTime()
{
global g_SpacePressDownTime
time_elapsed := A_TickCount - g_SpacePressDownTime
Return time_elapsed
}
; Return the hotkey symbols (ie !, #, ^ and +) for the modifiers that
; are currently activated
GetModifiers()
{
Modifiers =
GetKeyState, state1, LWin
GetKeyState, state2, RWin
state = %state1%%state2%
if state <> UU ; At least one Windows key is down.
Modifiers = %Modifiers%#
GetKeyState, state1, Alt
if state1 = D
Modifiers = %Modifiers%!
GetKeyState, state1, Control
if state1 = D
Modifiers = %Modifiers%^
GetKeyState, state1, Alt
GetKeyState, state1, Shift
if state1 = D
Modifiers = %Modifiers%+
Return Modifiers
}