Autoit | Detects whether a specified keyboard key is on or off and changes the tray icon

This time, we will introduce sample code that detects whether a specified keyboard key is on or off and changes the tray icon accordingly.

The main functions used are:

TraySetIcon = Loads/sets the specified tray icon.

_WinAPI_GetKeyState = Retrieves the status of the specified virtual key.

BitAND = Performs a bitwise AND operation.

table of contents

Sample code and explanation

*The Caps Lock key is specified in the sample.

To use the _WinAPI_GetKeyState function, you must include WinAPIEx.au3.

When specifying an image in the TraySetIcon function,extensionSpecify ".ico".

Images such as ".jpg" and ".bmp" cannot be used. *DLL files can also be specified.

You do not need to specify the full path to the image file as long as it is in the same location as the script.

#include <WinAPIEx.au3>

While 1
    If CapsLockState() Then
        TraySetIcon("on.ico")
    Else
        TraySetIcon(@ScriptDir & "\off.ico")
    EndIf
    Sleep(250)
WEnd

Func CapsLockState()
    Return BitAND(_WinAPI_GetKeyState(0x14), 1)
EndFunc   ;==>CapsLockState

BitAND

ユーザー定義関数

The value "0x14" specified in the _WinAPI_GetKeyState function is Caps Lock.Virtual Key Code will be important.

Virtual key codes are:

It is a hexadecimal number that allows the OS and programs to identify keystrokes.

Returns "1" if Caps Lock is on, "0" if Caps Lock is off.

Try downloading the icon file, moving it to the same location as the script, and running the above code.

ON – OFF icon.zip

The icon changes depending on whether Caps Lock is on or off.

Caps Lock is off
Caps Lock is off
Caps Lock is on
Caps Lock is on

Did you notice anything when you ran the above code?

This code changes the icon depending on whether Caps Lock is on or off, but TraySetIcon is executed multiple times, causing the tray icon to flash.

If CapsLockState() Then
        TraySetIcon("on.ico")
Else
        TraySetIcon(@ScriptDir & "\off.ico")
EndIf

So we add a condition.

If the variable $ico is "0", then... If the variable $ico is "1", then...

#include <WinAPIEx.au3>

$ico = 0

While 1
    If CapsLockState() Then
        If $ico = 0 Then
            TraySetIcon(@ScriptDir & "\on.ico")
            $ico = 1
        EndIf
    Else
        If $ico = 1 Then
            TraySetIcon(@ScriptDir & "\off.ico")
            $ico = 0
        EndIf
    EndIf
    Sleep(250)
WEnd

Func CapsLockState()
    Return BitAND(_WinAPI_GetKeyState(0x14), 1)
EndFunc   ;==>_GetCapsLockState

When Caps Lock is on, the value of the variable $ico is checked, and if it is "0", the tray icon is set to "on.ico" and the value of the variable $ico is set to "1".

Now, while Caps Lock is on, the value of the variable $ico is set to "1", so TraySetIcon(@ScriptDir & "\on.ico") will only be executed the first time.

This eliminates unnecessary processing and the flashing tray icon.

*If the sleep time during the loop is too short, it will put a strain on the CPU.

Although this looks complete, there is still a problem with this code.

The problem is that if Caps Lock is off when you start the program, the icon will not be set.

So, first determine whether Caps Lock is on or off and set the icon.

#include <WinAPIEx.au3>

$ico = 0
SetTrayIcon(); 初めに Caps Lock のオン・オフを判断してアイコンをセットする

While 1
    ; Caps Lockがオンならば
    If CapsLockState() Then
        If $ico = 0 Then
            ;トレイアイコンにアイコン "on.ico" をセット
            TraySetIcon("on.ico")
            $ico = 1
        EndIf
    ; Caps Lockがオフならば
    Else
        If $ico = 1 Then
            ;トレイアイコンにアイコン "off.ico" をセット
            TraySetIcon("off.ico")
            $ico = 0
        EndIf
    EndIf
    Sleep(250)
WEnd

; Caps Lock のオン・オフを判断
Func CapsLockState()
    ; 「0x14」が Caps Lock の仮想キーコード
    Return BitAND(_WinAPI_GetKeyState(0x14), 1)
EndFunc   ;==>CapsLockState

Func SetTrayIcon()
    If CapsLockState() Then
        TraySetIcon("on.ico")
    Else
        TraySetIcon("off.ico")
    EndIf
EndFunc   ;==>SetTrayIcon

Please try other virtual key codes besides Caps Lock.

If you found this article helpful, please share it on social media.

Person who wrote this article

Driven by questions arising from my daily PC use and the desire to "do more," I have been pursuing self-study in Windows since around 2008. I am sharing the "aha!" techniques and solutions I discovered through trial and error with the sole purpose of helping you in your PC life.

View profile

Comment:

To comment

[About submissions]
We welcome any questions or information regarding the content of the article.
However, please note that content unrelated to the purpose of the article, criticism of specific individuals or organizations, offensive language,Inappropriate wordsComments containing the above may be deleted or made private without notice at the discretion of the administrator.
Please note that spam may be automatically deleted by anti-spam measures.

CAPTCHA


table of contents