Jump to content

Make Caps Lock a modifier key


Recommended Posts

I would like to use Caps Lock, the key I never use, as a modifier key.

This however seems impossible to do. The key does not like working with other keys. Is there a way to make it work?

The reason is I ordered myself a mechanical keyboard without media keys. I'd really like to use the Caps Lock key as a modifier key to get back mediacontrols or run custom scripts in programs and games. I got it working in AutoHotkey, but that gives me bugs when using my IDE, since I use an IDE daily I'd really like to get it working with AutoIt.

Opt("SendCapslockMode", 0)
HotKeySet("{CAPSLOCK}1", "VolUp")
HotKeySet("{CAPSLOCK}2", "VolDown")

While 1
    Sleep(100)
WEnd

Func VolUp()
    Send("{VOLUME_UP}")
EndFunc

Func VolDown()
    Send("{VOLUME_DOWN}")
EndFunc
Link to comment
Share on other sites

  • Moderators

Dinovic,

Welcome to the AutoIt forum. ;)

You could try using _IsPressed within the HotKey function like this: :D

#include <Misc.au3>

Opt("SendCapslockMode", 0)
HotKeySet("1", "VolUp")
HotKeySet("2", "VolDown")

While 1
    Sleep(100)
WEnd

Func VolUp()
    If _IsPressed("14") Then
        ConsoleWrite("{VOLUME_UP}" & @CRLF)
    Else
        HotKeySet("1")
        Send("1")
        HotKeySet("1", "VolUp")
    EndIf
EndFunc

Func VolDown()
    If _IsPressed("14") Then
        ConsoleWrite("{VOLUME_DOWN}" & @CRLF)
    Else
        HotKeySet("2")
        Send("2")
        HotKeySet("2", "VolDown")
    EndIf
EndFunc

Any use? ;)

M23

Edit: Fixed the silly typo mentioned below - cut-and-paste is very handy but can catch you out! :)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Dinovic,

Welcome to the AutoIt forum. :)

You could try using _IsPressed within the HotKey function like this: ;)

#include <Misc.au3>

Opt("SendCapslockMode", 0)
HotKeySet("1", "VolUp")
HotKeySet("2", "VolDown")

While 1
    Sleep(100)
WEnd

Func VolUp()
    If _IsPressed("14") Then
        ConsoleWrite("{VOLUME_UP}" & @CRLF)
    Else
        HotKeySet("1")
        Send("1")
        HotKeySet("1", "VolUp")
    EndIf
EndFunc

Func VolDown()
    If _IsPressed("14") Then
        ConsoleWrite("{VOLUME_DOWN}" & @CRLF)
    Else
        HotKeySet("1")
        Send("2")
        HotKeySet("1", "VolDown")
    EndIf
EndFunc

Any use? ;)

M23

Thanks! What a nice and fast response. It works very nicely. Fixed a small typo in the VolDown function and replaced ConsoleWrite with Send and it works nicely.

Is there any way to disable the Caps Lock key completely so it doesn't turn on ever?

Link to comment
Share on other sites

Or in a compacted format:

#include <Misc.au3>
Opt("SendCapslockMode", 0)
HotKeySet("1", "_Volume")
HotKeySet("2", "_Volume")
While Sleep(10000)
WEnd
Func _Volume()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & "}")
    HotKeySet(@HotKeyPressed)
    Send(@HotKeyPressed)
    HotKeySet(@HotKeyPressed, "_Volume")
EndFunc   ;==>_Volume

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Even smaller :)

#include <Misc.au3>
$t = Opt("SendCapslockMode", 0) & HotKeySet("1", "_V") & HotKeySet("2", "_V")
While Sleep(10000)
WEnd
Func _V()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & "}")
    Return HotKeySet(@HotKeyPressed) & Send(@HotKeyPressed) & HotKeySet(@HotKeyPressed, "_V")
EndFunc   ;==>_V

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Thanks all, but is there a way to disable the the Caps Lock key completely or reverting it back to normal after holding the keys?

{CAPSLOCK UP} doesn't seem to work. I'd really like to revert it back to off after it fired the up event.

Edited by Dinovic
Link to comment
Share on other sites

Fixed the CAPSLOCK problem

#include <Misc.au3>
$t = Opt("SendCapslockMode", 0) & HotKeySet("1", "_V") & HotKeySet("2", "_V")
While Sleep(10000)
WEnd
Func _V()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & "}{CAPSLOCK UP}{CAPSLOCK DOWN}")
    Return HotKeySet(@HotKeyPressed) & Send(@HotKeyPressed) & HotKeySet(@HotKeyPressed, "_V")
EndFunc   ;==>_V

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Fixed the CAPSLOCK problem

#include <Misc.au3>
$t = Opt("SendCapslockMode", 0) & HotKeySet("1", "_V") & HotKeySet("2", "_V")
While Sleep(10000)
WEnd
Func _V()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & "}{CAPSLOCK UP}{CAPSLOCK DOWN}")
    Return HotKeySet(@HotKeyPressed) & Send(@HotKeyPressed) & HotKeySet(@HotKeyPressed, "_V")
EndFunc   ;==>_V

Thanks but it only works correctly when tapping the capslock key and the other key shortly, not when holding it. When you hold the keys it repeatedly blinks on and off and either stops with Caps Lock on or off when you're lucky.

Is there no real way to disable the Caps Lock key in front like AutoHotkey's "SetCapsLockState, alwaysoff"?

Link to comment
Share on other sites

OK, fixed the LED problem.

#include <Misc.au3>
$t = Opt("SendCapslockMode", 0) & HotKeySet("1", "_V") & HotKeySet("2", "_V")
While Sleep(100000)
WEnd
Func _V()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & _Iif(_KeyboardState()>3,"}{CAPSLOCK UP}{CAPSLOCK DOWN","")&"}")
    Return HotKeySet(@HotKeyPressed) & Send(@HotKeyPressed) & HotKeySet(@HotKeyPressed, "_V")
EndFunc   ;==>_V
Func _KeyboardState()
    $kp = DllStructCreate("ushort ;ushort L;")
    $Kernel32 = DllOpen("Kernel32.dll")
    DllCall("Kernel32.dll", "int", "DefineDosDeviceW", "dword", 1, "wstr", "Keybd", "wstr", "\Device\KeyboardClass0")
    $kb = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\Keybd", "dword", 0x40000000, "dword", 0, "dword", 0, "dword", 3, "dword", 0, "dword", 0)
    $kb = $kb[0]
    DllCall($Kernel32, "int", "DeviceIoControl", "hwnd", $kb, "dword", 720960, "ptr", 0, "dword", 0, "ptr", DllStructGetPtr($kp), "dword", DllStructGetSize($kp), "dword*", 0, "ptr", 0)
    DllCall("Kernel32.dll", "int", "DefineDosDeviceW", "dword", 2, "wstr", "Keybd", "wstr", "")
    DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $kb)
    DllClose($Kernel32)
    Return DllStructGetData($kp, "L")
    ; SCROLL = 1   NUM = 2   CAPS = 4
EndFunc   ;==>_KeyboardState

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

OK, fixed the LED problem.

#include <Misc.au3>
$t = Opt("SendCapslockMode", 0) & HotKeySet("1", "_V") & HotKeySet("2", "_V")
While Sleep(100000)
WEnd
Func _V()
    If _IsPressed("14") Then Return Send("{VOLUME_" & _Iif(@HotKeyPressed - 1, "UP", "Down") & _Iif(_KeyboardState()>3,"}{CAPSLOCK UP}{CAPSLOCK DOWN","")&"}")
    Return HotKeySet(@HotKeyPressed) & Send(@HotKeyPressed) & HotKeySet(@HotKeyPressed, "_V")
EndFunc   ;==>_V
Func _KeyboardState()
    $kp = DllStructCreate("ushort ;ushort L;")
    $Kernel32 = DllOpen("Kernel32.dll")
    DllCall("Kernel32.dll", "int", "DefineDosDeviceW", "dword", 1, "wstr", "Keybd", "wstr", "\Device\KeyboardClass0")
    $kb = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\Keybd", "dword", 0x40000000, "dword", 0, "dword", 0, "dword", 3, "dword", 0, "dword", 0)
    $kb = $kb[0]
    DllCall($Kernel32, "int", "DeviceIoControl", "hwnd", $kb, "dword", 720960, "ptr", 0, "dword", 0, "ptr", DllStructGetPtr($kp), "dword", DllStructGetSize($kp), "dword*", 0, "ptr", 0)
    DllCall("Kernel32.dll", "int", "DefineDosDeviceW", "dword", 2, "wstr", "Keybd", "wstr", "")
    DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $kb)
    DllClose($Kernel32)
    Return DllStructGetData($kp, "L")
    ; SCROLL = 1   NUM = 2   CAPS = 4
EndFunc   ;==>_KeyboardState

Thanks, but it didn't work, but I worked around it by using a tool called KeyTweak. It allows you to remap your keyboard keys real easily. So I replaced the Caps Lock with a key not found on my keyboard(without a numpad). Didn't keep the script short like you since I won't use the key just for volume, but also more advanced scripts.

#include <Misc.au3>
HotKeySet("{NUMPADMULT}", "HoldModifier")

While 1
    Sleep(100)
WEnd

Func HoldModifier()
    If _IsPressed("6A") Then
        HotKeySet("1", "VolUp")
        HotKeySet("2", "VolDown")       
        HotKeySet("3", "VolMute")
        while _IsPressed("6A")
            sleep(0)
        WEnd
        HotKeySet("1")
        HotKeySet("2")      
        HotKeySet("3")
    EndIf   
EndFunc

Func VolUp()
    Send("{VOLUME_UP}")
EndFunc

Func VolDown()
    Send("{VOLUME_DOWN}")
EndFunc

Func VolMute()
    Send("{VOLUME_MUTE}")
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...