📜 ⬆️ ⬇️

CapsLock as an additional modifier

Introduction


I was uncomfortable pressing Ctrl-F4 to close a tab in VisualStudio, I tried to reassign this function to the standard for Internet browsers and some other programs Ctrl-W, but I ran into difficulties and decided to find a more flexible solution.

On Habré there are already articles devoted to Caps Lock, and probably the most canonical option is to use it to switch layouts. I want to show a more flexible and, at the same time, quite simple approach.

Idea


And why is CapsLock worse than Shift / Ctrl / Alt? Why not use it in the same way? So, the idea is this: write a program that will track the pressing / release of the caps and the pressing of some other keys in the interval between these two events and perform some action based on the result.

AutoIt


AutoIt is a free scripting language for automating task execution in Windows. To start working with it, just download AutoIt Full Installation , install and run a truncated version of the SciTE editor that comes in the package.
')

Source Code


Code in one piece and without comments
#include <Misc.au3> Global $isSingleCaps = True HotKeySet("{LAUNCH_MAIL}", "HoldModifier") While 1 Sleep(60000) WEnd Func HoldModifier() $isSingleCaps = True If _IsPressed("B4") Then HotKeySet("w", "CloseTab") HotKeySet("t", "RunAllTests") HotKeySet("]", "RestartExplorer") HotKeySet("a", "Play") HotKeySet("{NUMPADSUB}", "VolumeUp") HotKeySet("0", "RealCaps") While _IsPressed("B4") sleep(0) WEnd Unset() EndIf If $isSingleCaps Then Send("!+9") EndIf EndFunc Func Unset() HotKeySet("w") HotKeySet("t") HotKeySet("]") HotKeySet("a") HotKeySet("{NUMPADSUB}") HotKeySet("0") EndFunc Func CloseTab() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Microsoft Visual Studio", 1, -1) Then Send("^{F4}") Else Send("^{w}") EndIf EndFunc Func RunAllTests() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Microsoft Visual Studio", 1, -1) Then Send("^u") Send("^l") EndIf EndFunc Func RestartExplorer() $isSingleCaps = False ShellExecute("taskkill", "/im explorer.exe /f") ProcessWaitClose("explorer.exe") Run("C:\Windows\explorer.exe") EndFunc Func VolumeUp() $isSingleCaps = False Send("{VOLUME_UP 3}") EndFunc Func Play() Unset() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Total Commander", 1, -1) Then Send("{RIGHT}") Send("+{RIGHT}") Send("aimp3.exe /add_play ") Send("{BACKSPACE}") Send("^p") Send("{ENTER}") Send("{ENTER}") Send("{LEFT}") EndIf EndFunc Func RealCaps() Unset() $isSingleCaps = False $var = InputBox("To UpperCase", "     UpperCase") Send(StringUpper($var)) EndFunc 



 #include <Misc.au3> ;  Misc.au3   _IsPressed,     Global $isSingleCaps = True ;        HotKeySet("{LAUNCH_MAIL}", "HoldModifier") ;     ,       While 1 Sleep(60000) ; " " WEnd 


The HotKeySet function can only accept a specific set of keys and handles the CapsLock / NumLock / ScrollLock switch keys in a special way, so to simplify, CapsLock is reassigned to me via {LAUNCH_MAIL} through the registry ( SharpKeys is a free utility that, in essence, is a graphical interface to reassign keys through the registry). There are more advanced versions of HotKeySet, but I didn’t want to complicate the example.

The infinite loop shows an example of the “event-based execution model” for AutoIt: when you call a function assigned to a hot key, the execution of the payload stops and continues after the end of the function. Later I will show another option.

 Func HoldModifier() $isSingleCaps = True If _IsPressed("B4") Then ; _IsPressed            HotKeySet("w", "CloseTab") ;    ,      HotKeySet("t", "RunAllTests") HotKeySet("]", "RestartExplorer") HotKeySet("a", "Play") HotKeySet("{NUMPADSUB}", "VolumeUp") HotKeySet("0", "RealCaps") While _IsPressed("B4") ;    sleep(0) WEnd Unset() EndIf If $isSingleCaps Then ;  $isSingleCaps   , ,          Send("!+9") ; "!" - Alt, "+" - Shift,  Alt+Shift+9           EndIf EndFunc Func Unset() HotKeySet("w") ; HotKeySet        HotKeySet("t") HotKeySet("]") HotKeySet("a") HotKeySet("{NUMPADSUB}") HotKeySet("0") EndFunc 


This ends the fundamental logic of using CapsLock, as I wanted to show, further examples of what can be done with AutoIt.

Closing tabs in VS:

 Func CloseTab() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Microsoft Visual Studio", 1, -1) Then ;    Send("^{F4}") Else Send("^{w}") ;         Caps+W EndIf EndFunc 


Run all tests:

 Func RunAllTests() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Microsoft Visual Studio", 1, -1) Then Send("^u") Send("^l") EndIf EndFunc 


Restart the explorer process (it helped, until the Win8 activation servers were running):

 Func RestartExplorer() $isSingleCaps = False ShellExecute("taskkill", "/im explorer.exe /f") ProcessWaitClose("explorer.exe") Run("C:\Windows\explorer.exe") EndFunc 


I did not find how to change the volume change step using the multimedia keys in the eighth Windows, so I went this way:

 Func VolumeUp() $isSingleCaps = False Send("{VOLUME_UP 3}") ;  {VOLUME_UP}   EndFunc 


Send to play in AIMP the folder where the cursor is in TotalCommander (Ctrl + P - sends the path to the current folder to the command line, I did not find the path to the folder under the cursor, so I got this crooked but working version (build PowerUser, if it matters)):

 Func Play() Unset() $isSingleCaps = False If StringInStr(WinGetTitle("[active]"), "Total Commander", 1, -1) Then Send("{RIGHT}") ;    Send("+{RIGHT}") ;       Send("aimp3.exe /add_play ") ;        Send("{BACKSPACE}") ;  /   Send("^p") ;      Send("{ENTER}") Send("{ENTER}") Send("{LEFT}") ;     EndIf EndFunc 


The CapsLock function as it is.

 Func RealCaps() Unset() $isSingleCaps = False $var = InputBox("To UpperCase", "     UpperCase") Send(StringUpper($var)) EndFunc 


Other performance model


In general, it looks like this:

 While 1 If _IsPressed("B4") Then ... EndIf Sleep(50) WEnd 


It requires a little more resources, but allows you to use any virtual code.

Conclusion


The final step is to compile the program Ctrl + F7 and place the link to the resulting file in the startup folder.

A similar mechanism, of course, can be implemented in other programming languages ​​and for other operating systems; there is no rocket science here.

This method has several significant positive qualities:

Source: https://habr.com/ru/post/150754/


All Articles