Jump to content

[Solved] Windows Hotkeys


Recommended Posts

I've had to migrate from AutoHotKey to AutoIt due to technical limitations, and I can't get my favourite, most-used script that I use every day, to work because Windows Hotkey Combinations work in AutoHotKey, but not in AutoIt. I need to figure out how to amend this.

The script itself doesn't have much to do with the topic at hand (centers a window), but rather how each program registers its hotkeys. In AHK, for instance, registering a hotkey for the non-reserved combination "Windows + C" works splendidly. All you have to define is:

#c::

In AutoIt, registering the same hotkey for the non-reserved combination doesn't work at all (returns 0 indicating it failed to set the hotkey):

HotKeySet("#c", "_Center")

I made sure that during testing, the hotkey isn't consumed by another app / script already.

That begs the question, what does AHK do differently in this regard and how can I get my hotkey + script to work just like it does in AHK?

Thanks to anyone willing to help in advance. 

Edited by Medallyon
Link to comment
Share on other sites

2 hours ago, Exit said:

#c is reserved by Cortana

It might be, but why is AutoHotKey able to execute it? According to this page, AHK manages to override the windows keys somehow. Any idea on how that could be applicable in AutoIt?

I may just end up disabling all the hotkeys by following the first instruction on the page I linked and re-implement the default Windows hotkeys that I want. I would love to find an alternative, though. Maybe some registry entries that disable specific windows hotkeys that could be implemented in a UDF?

Link to comment
Share on other sites

6 minutes ago, Medallyon said:

Maybe some registry entries that disable specific windows hotkeys that could be implemented in a UDF?

I was right! I did some research and found this, which essentially does exactly what I want. I found out that this method requires a restart of the Windows Explorer, which I can live with, I guess.

If I have the time & energy, I may even post some sort of UDF in here 😃

Link to comment
Share on other sites

#cs
    AutoIt Version: 3.3.14.5
    Author:         Medallyon

    Script Function:
        Enable or Disable a Windows HotKey. Does not work for *reserved* reserved keys 'L' and 'U'.

    API:
        Settings:
         > [Bool] $dwhk_setting_verbose: Whether the program should spew debugging strings to the console.

        [Bool] _IsHotKeyDisabled($letter)
         > Returns 1 if the specified HotKey is already in the "DisabledHotkeys" list.

        [Array] _FetchDisabledHotKeys()
         > Fetch all hotkeys that are currently disabled.

        [Void] _DisableWindowsHotKey($letter, $refresh = True)
         > Disable the given $letter. Pass a truthy value for $refresh to restart explorer.exe for the change to take effect immediately.

        [Void] _EnableWindowsHotKey($letter, $refresh = True)
         > Enable the given $letter. Pass a truthy value for $refresh to restart explorer.exe for the change to take effect immediately.
#ce

#include <array.au3>

; Constants
Local $REG_PATH = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
Local $ALPHABET = " 0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ"

; Settings
Local $dwhk_setting_verbose = True

; <========== Private Functions
Func _PRINT($msg)
    If (Not $dwhk_setting_verbose) Then Return
    ConsoleWrite("[DisableWindowsHotKey] " & $msg & @CRLF)
EndFunc

Func _FETCH_DISABLED_HOTKEYS()
    Local $existing = RegRead($REG_PATH, "DisabledHotkeys")

    If (@error = 0) Then
        Return $existing
    ElseIf (@error = -1) Then
        Return ""
    Else
        _PRINT("Encountered an error trying to read the Registry.")
        Return 1
    EndIf
EndFunc

Func _WRITE_DISABLED_HOTKEYS($keys)
    $aKeys = StringSplit($keys, "", $STR_NOCOUNT)
    _ArraySort($aKeys)
    _ArrayDisplay($aKeys)
    $keys = _ArrayToString($aKeys, "", -1, -1, "")
    _PRINT($keys)

    If (StringLen($keys) = 0) Then
        If (RegDelete($REG_PATH, "DisabledHotkeys") = 2) Then
            _PRINT("Encountered an error trying to remove the HotKeys from the Registry.")
            Return 1
        EndIf

        Return 0
    ElseIf (RegWrite($REG_PATH, "DisabledHotkeys", "REG_SZ", $keys) = 0) Then
        _PRINT("Encountered an error trying to write to the Registry.")
        Return 1
    EndIf

    Return 0
EndFunc

Func _REFRESH_EXPLORER()
    Local $pidExplorer = ProcessExists("explorer.exe")
    If ($pidExplorer = Not 0) Then
        ProcessClose($pidExplorer)
        ProcessWaitClose($pidExplorer, 3)
    EndIf

    Return RunWait("explorer.exe", 3)
EndFunc
; ==========> Private Functions



; <========== API
Func _IsHotKeyDisabled($letter)
    Return StringInStr(_FETCH_DISABLED_HOTKEYS(), $letter)
EndFunc

Func _FetchDisabledHotKeys()
    Return StringSplit(_FETCH_DISABLED_HOTKEYS(), "")
EndFunc

Func _EnableWindowsHotKey($letter, $refresh = True)
    Local $disabled = _FETCH_DISABLED_HOTKEYS()

    $letter = StringUpper($letter)
    If (Not StringInStr($disabled, $letter, True)) Then
        _PRINT("This Windows HotKey (" & $letter & ") is already enabled.")
        If ($refresh) Then _REFRESH_EXPLORER()
        Return 0
    EndIf

    $disabled = StringReplace($disabled, $letter, "")
    _PRINT($disabled)
    _WRITE_DISABLED_HOTKEYS($disabled)

    If ($refresh) Then _REFRESH_EXPLORER()
    _PRINT("Successfully removed '" & $letter & "' from DisabledHotkeys.")
    Return 0
EndFunc ; ===> _EnableWindowsHotKey

Func _DisableWindowsHotKey($letter, $refresh = True)
    Local $disabled = _FETCH_DISABLED_HOTKEYS()

    $letter = StringUpper($letter)
    If (StringInStr($disabled, $letter, True)) Then
        _PRINT("This Windows HotKey (" & $letter & ") is already disabled.")
        If ($refresh) Then _REFRESH_EXPLORER()
        Return 0
    EndIf

    If (Not StringInStr($ALPHABET, $letter, True)) Then
        _PRINT("Ensure that the Windows HotKey being disabled is not a special character.")
        Return 1
    EndIf

    $disabled &= $letter
    _WRITE_DISABLED_HOTKEYS($disabled)

    If ($refresh) Then _REFRESH_EXPLORER()
    _PRINT("Successfully added '" & $letter & "' to DisabledHotkeys.")
    Return 0
EndFunc ; ===> _DisableWindowsHotKey
; ==========> API

 

Edited by Medallyon
Improvements
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...