Remap alphabetic keys to numeric when NumLock is On with AutoHotKey - autohotkey

I have a laptop DELL XPS 13 9310 that doesn't come with physical or togglable numeric keys and visible NumLock key. However, I have just discovered that I can press Fn+B to toggle the NumLock state as can be seen on a virtual keyboard.
Can AHK detect the NumLock state and rewrite M JKL UIO to 0 123 456?
And how do I write this detection mechanism?

I woul'd try something like:
#Persistent
#if GetKeyState("NumLock", "T")
ToolTip % GetKeyState("NumLock", "T")
M::
Send, 0
return
J::
Send, 1
return
K::
Send, 2
return
L::
Send, 3
return
U::
Send, 4
return
I::
Send, 5
return
O::
Send, 6
return
This is a bit grost, but, probably will do the job

Related

Change a key function for x amount of time?

So thanks to this user i have got this working code, but i want to further tweak it if its posibble. :D
Here is the code:
global s:=0, c:=0, t:=1500, t2:=380
*lbutton::
send % Seqkeys("5","6")
KeyWait, lbutton
If (A_TickCount-s < t2)
c := 0
Send, 7
return
Seqkeys(params*) {
global s, c, t
max := params.MaxIndex()
(A_TickCount-s<=t && (c+=1)<=max) ? c : c:=1
s := A_TickCount
return params[c]
}
What i want to implement is that if i hit the right mouse button (Rbutton), the original lbutton seqkeys code changes to this code for 1 second
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
return
than when the 1 sec is over the code is revert back to the original seqkeys state.
I got this code sample but its not working properly for several reasons, when i hit the Rbutton it does override the original Seqkeys function, but it never resets back to the seqkeys one. I put it here maybe it helps
*rbutton::
toggle:=true
return
#If Toggle
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
return
toggle:=false
Return
#If
Thx again! :)
The reason why it never reset back to Seqkeys(params*) after toggle is that toggle:=false is outside the toggled lbutton hotkey.
Putting it inside toggle lbutton hotkey before the return statement fixes the issue.
#If Toggle
*lbutton::
send, 8
KeyWait, lbutton
Send, 7
toggle:=false
return

Layer based Keyboard using AutoHotKey: Change modifiers single press, hold, and double press behavior

folks,
I want to create a layer based keyboard using AutoHotkey. Basicly, I want to achieve what shift already does: modify each key when a modifier is used.
I want to improve regular shift in the following:
press modifier once: only change layer for next character
hold modifier: change layer as long as modifier is down
press modifier twice: enter layer mode, like capslock. (end by another press)
Modifiers: LAlt, RAlt, LControl, RControl (CapsLock, Shift)
How cas I accomplish this?
what I found so far on stackoverflow:
This code allows for shift to be pressed and released for the next character
$*LShift::
SendInput, {LShift Down} ; press shift
Input, Key, L1 M V ; wait for input character
If GetKeyState("LShift", "P") ; if shift still pressed, wait for release
KeyWait, LShift
SendInput, {LShift Up} ; send input with shift down, the shift up
Return
this code turns a double shift press into CapsLock
LShift::
KeyWait, CapsLock ; wait to be released
KeyWait, CapsLock, D T0.2 ; and pressed again within 0.2 seconds
if ErrorLevel
return
else if (A_PriorKey = "CapsLock")
SetCapsLockState, % GetKeyState("CapsLock","T") ? "Off" : "On"
return
#If, GetKeyState("CapsLock", "P") ; hotkeys go below
a::b
#If
But I am not experienced enough with AHK to bring this together. My goal is to have something like
Modifier::
; code that makes the modifier behave like expected: single press, hold, double press
Return
#If, GetKeyState("Modifier", "P") ; List of key remaps in specific layer
#If
I hope this is specific enough and that you can help me out here.
thanks!
Assign the corresponding Booleam values (true or false) to the variables "Double_LAlt" and "Double_LAlt_holding" in order to create context-sensitive hotkeys depended on their values:
LAlt::
ToolTip,,,, 3
ToolTip,,,, 4
Double_LAlt := false
; Press twice or press twice and hold LAlt within 0,2 seconds
If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 200)
{
Sleep, 200
If GetKeyState("LAlt","P")
{
ToolTip,,,, 4
ToolTip, Double_LAlt_holding,,, 2
Double_LAlt_holding := true
}
else
{
ToolTip,,,, 4
ToolTip, Double_LAlt,,, 3
Double_LAlt := true
}
}
If !((Double_LAlt_holding) || (Double_LAlt)) ; "!" means "NOT" and "||" means "OR"
ToolTip, LAlt_holding,,, 1
return
~LAlt Up::
ToolTip,,,, 1
ToolTip,,,, 2
Double_LAlt_holding := false
Sleep, 100
If (A_TimeIdlePhysical > 100)
Tooltip, PriorHotKey = LAlt Up,,, 4
SetTimer, RemoveTooltip, 1000
return
#If (Double_LAlt_holding) ; If this variable has the value "true"
<!a:: MsgBox, a while Double_LAlt_holding ; "<!" means "LAlt"
<!1:: MsgBox, 1 while Double_LAlt_holding
#If (Double_LAlt)
a:: MsgBox, a after Double_LAlt
1:: MsgBox, 1 after Double_LAlt
; Press a key within 2 seconds after releasing LAlt:
#If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 2000)
a:: MsgBox, a after LAlt Up
1:: MsgBox, 1 after LAlt Up
#If GetKeyState("LAlt","P")
a:: MsgBox, a while LAlt_holding
1:: MsgBox, 1 while LAlt_holding
#If
RemoveTooltip:
If (A_TimeSincePriorHotkey > 2000) ; 2 seconds
ToolTip,,,, 4
return

How to use HotString with variable & wildcard?

In Onenote, let's say I want to write x^y (x to power of y), I would need to enter Equ mode (by keyboard shortcut) , type x^y , hit Space, then exit equ mode
So, I figure it could be something like this :
::(wild_card)^(wild_card)::
Send, (Shortcut for equ mode)
Send, variable1
Send, ^
Send, variable2
Send, space
Send, (Shortcut for equ mode)
How do I actually achieve this ?
Alternatively, the same purpose could be formulated by doing,
- monitor word separated by Ending chars
- detect if ^ is in those word
- Send (Shortcut) , then the word, then space, then (Shortcut).
Is this more doable ?
Thanks MCL,
Here is the code for auto convert x_y and x^y into their mathematical representation in onenote
#Include DynamicHotstrings.ahk
#IfWinActive, ahk_class Framework::CFrame
hotstrings("(\w+\_\w+) ","dowork")
hotstrings("(\w+\^\w+) ","dowork")
dowork:
Send, {LAlt Down}{=}{LAlt Up}
SendRaw, %$1%
Send, {Space}
Send, {LAlt Down}{=}{LAlt Up}
Send, {Space}
Return
Better yet , you can modify the DynamicHotstrings.ahk so it will ignore auto convert when we are already in equation mode:
hotstrings(k, a = "")
{
CoordMode,Pixel,Screen
PixelGetColor, color, 455, 1
if (color = 0xD7337F)
return
....

Shift Key Training Wheels and Shift Parenthesis Remap

I'm looking to use AutoHotKey to modify the functionality of my shift keys. The functionality is described in Steve Losh's Blog entry here. Specifically, I'd like my shift keys to do the following:
If LShift or RShift is pressed and released in under 300 ms with no other keys being pressed in between, send ( or ), respectively.
If LShift and RShift are "rolled" together (press LShift, press RShift, release LShift, release RShift, etc.) in under 300ms, send () or )(.
If a shift key is used improperly (LShift and S, RShift and K, etc.) then nothing happens.
I've been having issues with the 300ms requirement and the "rolling" functionality. Specifically, I'm having issues with only being able to detect when the key is released due to the hotkey combos such as:
LShift & 0:: return
This is where I'm at so far:
LShift::
Send {LShift Down}
KeyWait, LShift
Send {LShift Up}
if (A_TimeSinceThisHotkey < 300){
if (A_PriorKey = "LShift")
{
Send {)}
}
}
return
I don't see a reason to use a 300 ms timeout anyway, it seems unreliable and unnecessary.
Have a look at this commented code, it is short and efficient, and seems to meet all of your requirements:
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
/* Put your unwanted combinations here
* and let them do nothing
*/
LShift & q::return
RShift & y::return
Edit:
Since LShift and RShift already are prefix hotkeys, I left out the trick described here.
MCL's answer is close but when I tested it I found that shift-clicking didn't select text. Here's a version with a passthrough to allow shift-clicking to work.
;shift to parens
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
;passthrough for shift-click
LShift & LButton::
Send, {LShift Down}{LButton}
Send, {LShift Up}
RShift & LButton::
Send, {RShift Down}{LButton}
Send, {RShift Up}
I don't think the 300ms timeout is possible without either very deep understanding of autohotkey's implementation or actual modification to autohotkey. When I tried to get it to work (using http://www.autohotkey.com/board/topic/98742-remapping-shift-key/) I found that A_PriorHotkey was not consistently populated. I don't think that variable was meant to work with modifier keys this way.
I felt compelled to figure this one out. Here you go!
I basically created a hotkey for every Shift + Letter key combination in order to send the correct key case and also set the Abort value. The Abort value is then referenced whenever one of the Shift keys is pressed in order to determine whether or not to send the corresponding ( or ).
The "Rolling" was accomplished by creating a Hotkey for LShift + RShift (and the opposite). It then looks to see which key is released first to determine () or )(.
Accept if this was what you were looking for!
Loop 26
{
Hotkey, % "~LShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
Hotkey, % "~RShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
}
Return
RShift::
LShift::
Abort := 0
keyDown := A_TickCount
Keywait, % A_ThisHotkey
duration := A_TickCount - keyDown
If (duration < 200) and (Abort = 0)
SendInput % (A_ThisHotkey = "LShift") ? "(" : ")"
Send {LShift Up}
Return
RShift & LShift::
LShift & RShift::
Keywait, LShift
If GetKeyState("RShift", "P")
{
Keywait, RShift
Send ()
}
Else
Send )(
Return
LetterKey:
SendInput, % "+" SubStr(A_ThisHotKey, 0, 1)
Abort := 1
Return
EDIT:
Hmm, I seem to be having the same problem as you. I always get a duration of 0 due to the hotkeys.
I found and modified this script on the AutoHotKey Forums. (The original script was prone to type "K(" when you meant to type "K" if you type too quickly, so I've modified it so that shouldn't happen any more)
$LShift Up::send, % getkeystate("LShift") ? "{LShift Up}" : ""
$RShift Up::send, % getkeystate("RShift") ? "{RShift Up}" : ""
~$LShift::
KeyWait, LShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "LShift")
{
send, % getkeystate("LShift") ? "{LShift Down}" : "("
}
KeyWait, LShift
return
~$RShift::
KeyWait, RShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "RShift")
{
send, % getkeystate("RShift") ? "{RShift Down}" : ")"
}
KeyWait, RShift
return

Capslock + s + m in AutoHotKey

My full goal is to be able to hold down Capslock + s, which will convert the keys uiojklm,. to work like the 10-key number pad.
So as a first step, I am trying to map Capslock + s + m to the number 1
SetCapslockState AlwaysOff
Capslock & s::
keywait, m, d, t0.6
If (!ErrorLevel) {
SendInput {1}
} Return
I based my current code off of the answer here: Alt + Space + key in autohotkey
When I press Capslock + s + m, it prints out m1. How do I stop the m from printing?
Here is an alternative solution. You MUST have AutoHotKey_L for this to work since the traditional AutoHotKey does not support #if.
CapsLock & s::
Flag:=!Flag
If (Flag)
TrayTip, AutoHotKey, Numpad ON, 1
Else
TrayTip, AutoHotKey, Numpad OFF, 1
Return
#If (Flag)
m::Send, 0
k::Send, 1
#If
In the first block you toggle a Flag to True/False with CapsLock + s and you show the status with a traytip, then you define the behaviour of certain keys in the next block. Alternatively you could delete the first block and replace the #if (Flag) line with:
#If (GetKeyState("CapsLock", "P") and GetKeyState("s", "P"))
Update:
Tried the following with varying results. The first (commented out) code does use CapsLock + s, but apparently pressing the s key prevents AutoHotKey from seeing certain other key presses (here the letters n,m,i,o,p worked but j,k,l which are on the same hight/scanline on the keyboard were NOT detected)
SetCapsLockState, alwaysoff
/*
Capslock & s::
While, (GetKeyState("CapsLock", "P") and GetKeyState("s", "P"))
{
Input, MyKey, I L1 T0.5
TrayTip, Key:, %MyKey%
if (MyKey = "m")
Send, 1
if (MyKey = "i")
Send, 2
if (MyKey = "k")
Send, 3
if (MyKey = "j")
Send, 4
if (MyKey = "o")
Send, 5
if (MyKey = "p")
Send, 6
}
Return
*/
Just using CapsLock (also on the same like as j,k,l) worked, but that is not what you wanted.
Capslock::
While, (GetKeyState("CapsLock", "P"))
{
Input, MyKey, I L1 T0.5
TrayTip, Key:, %MyKey%
if (MyKey = "m")
Send, 1
if (MyKey = "i")
Send, 2
if (MyKey = "k")
Send, 3
if (MyKey = "j")
Send, 4
if (MyKey = "o")
Send, 5
if (MyKey = "p")
Send, 6
}
Return