Jump to content

Is it possible to use a Numpad0 + Numpad2 to activate a script ?


Recommended Posts

Hi.
I was wondering if it is possible to use a combination of keys to run a command. I have this code here that I use to switch between Headphones and Speakers with a Pause/Break key at the moment:

#include <GUIConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>
#include <Misc.au3>

;HotKeySet("{End}", "End")
;HotKeySet("{PGUP}", "SwitchSound")
;HotKeySet("{PGDN}", "ToggleShow")

Global $SOUNDBLASTER_WIN_TITLE = "[TITLE:Sound Blaster Command; REGEXPCLASS:HwndWrapper.*]"
;lobal $locked = False

Initialize()
MainLoop()

Func Initialize()
    Opt("WinWaitDelay", 0)
    AutoItSetOption("MouseCoordMode", 0)
    If ProcessExists("Creative.SBCommand.exe") Then
        ProcessClose("Creative.SBCommand.exe")
        Sleep(3000)
    EndIf
    Run("C:\Program Files (x86)\Creative\Sound Blaster Command\Creative.SBCommand.exe")

    $hid_soundblaster = False
    While Not $hid_soundblaster
        Sleep(100)
        If WinExists($SOUNDBLASTER_WIN_TITLE) And BitAND(WinGetState($SOUNDBLASTER_WIN_TITLE), $WIN_STATE_VISIBLE) Then
            Sleep(1000)
            WinSetState($SOUNDBLASTER_WIN_TITLE, "", @SW_HIDE)
            $hid_soundblaster = True
        EndIf
    WEnd
EndFunc

Func MainLoop()
    While True
        If _IsPressed(13) Then ; PAUSE/BREAK key.
            ;$locked = True
            SwitchSound()
        EndIf
        Sleep(100)
    WEnd
EndFunc

Func End()
    Exit
EndFunc

Func SwitchSound()
    $saved_win_handle = WinGetHandle("[active]")

    WinSetState($saved_win_handle, "", @SW_MINIMIZE)

    WinSetState($SOUNDBLASTER_WIN_TITLE, "", @SW_SHOW)
    WinActivate($SOUNDBLASTER_WIN_TITLE)
    WinWaitActive($SOUNDBLASTER_WIN_TITLE)
    MouseClick("left", 53, 622, 1, 0)
    WinSetState($SOUNDBLASTER_WIN_TITLE, "", @SW_HIDE)

    WinActivate($saved_win_handle)
EndFunc

Func ToggleShow()
    If Not BitAND(WinGetState($SOUNDBLASTER_WIN_TITLE), $WIN_STATE_VISIBLE) Then
        WinSetState($SOUNDBLASTER_WIN_TITLE, "", @SW_SHOW)
    Else
        WinSetState($SOUNDBLASTER_WIN_TITLE, "", @SW_HIDE)
    EndIf
EndFunc

I don't really understand the commands since I found this script online, but I tried Entering {Numpad0+Numpad2} instead of " PAUSE/BREAK key.", but it doesn't work. Also would this break the normal functionality of Numpad0 and Numpad2 ?

Thanks a lot and sorry for a noob question :)

Link to post
Share on other sites

PAUSE/BREAK key is a single key.  You want to replace it with 2 keys, how would that work ?  A single key can act easily as an On/Off toggle key, but 2 keys is not really appropriate in my sense.  FYI _IsPressed does not grab the key, it lets its normal functionality goes on.  On the contrary HotKeySet will eat the key, but there is a way to let it go normally (but with a noticeable delay).

Link to post
Share on other sites
#include <WinAPIvkeysConstants.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Global $hWndKeybd = GUICreate('')
Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
DllStructSetData($tRID, 'UsagePage', 0x01) ; Generic Desktop Controls
DllStructSetData($tRID, 'Usage', 0x06) ; Keyboard
DllStructSetData($tRID, 'Flags', $RIDEV_INPUTSINK)
DllStructSetData($tRID, 'hTarget', $hWndKeybd)
_WinAPI_RegisterRawInputDevices($tRID)
GUIRegisterMsg($WM_INPUT, WM_INPUT)
GUISetState(@SW_SHOW)

Do
Until GUIGetMsg()=-3

Func MyAction()
     MsgBox(0,'','hello')
EndFunc

Func KeystrokeLogic($tRIK)
     Switch $tRIK.VKey
       Case $VK_NUMPAD0
         If _WinAPI_GetAsyncKeyState($VK_NUMPAD2) and BitAnd(1,$tRIK.Flags) Then MyAction()
       Case $VK_NUMPAD2
         If _WinAPI_GetAsyncKeyState($VK_NUMPAD0) and BitAnd(1,$tRIK.Flags) Then MyAction()
     EndSwitch
EndFunc

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam)
        #forceref $iMsg, $wParam
     Switch $hWnd
       Case $hWndKeybd
            Local $tRIK = DllStructCreate($tagRAWINPUTKEYBOARD)
            If _WinAPI_GetRawInputData($lParam, $tRIK, DllStructGetSize($tRIK), $RID_INPUT) Then KeystrokeLogic($tRIK)
     EndSwitch
     If $wParam Then Return 0
EndFunc

 

Edited by AutoXenon
Link to post
Share on other sites
On 10/30/2022 at 2:28 AM, Nine said:

PAUSE/BREAK key is a single key.  You want to replace it with 2 keys, how would that work ?  A single key can act easily as an On/Off toggle key, but 2 keys is not really appropriate in my sense.  FYI _IsPressed does not grab the key, it lets its normal functionality goes on.  On the contrary HotKeySet will eat the key, but there is a way to let it go normally (but with a noticeable delay).

Sorry for late response, I was sure I'd be getting notifications on my e-mail as default.

Well I kind of don't wanna lose the functionality of either Numpad key, because I'm using numpad, that's the reason for the two-key stroke. Something like a Shift + Alt + F11 would work as well for example. Or just Shift + F11 if that one is not possible.

I didn't make this code, I just found it online, and wanted to customize the key itself. It essentially alttabs, puts the Sound Blaster Command window in front, clicks the Headphone/speaker toggle, and tabs back in the game.

 

EDIT: And I don't really care about the small delay, it needs a bit less than 0.5 second right now to do that and it's fine.

Edited by TuN
Link to post
Share on other sites
On 10/30/2022 at 3:27 PM, AutoXenon said:
#include <WinAPIvkeysConstants.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Global $hWndKeybd = GUICreate('')
Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
DllStructSetData($tRID, 'UsagePage', 0x01) ; Generic Desktop Controls
DllStructSetData($tRID, 'Usage', 0x06) ; Keyboard
DllStructSetData($tRID, 'Flags', $RIDEV_INPUTSINK)
DllStructSetData($tRID, 'hTarget', $hWndKeybd)
_WinAPI_RegisterRawInputDevices($tRID)
GUIRegisterMsg($WM_INPUT, WM_INPUT)
GUISetState(@SW_SHOW)

Do
Until GUIGetMsg()=-3

Func MyAction()
     MsgBox(0,'','hello')
EndFunc

Func KeystrokeLogic($tRIK)
     Switch $tRIK.VKey
       Case $VK_NUMPAD0
         If _WinAPI_GetAsyncKeyState($VK_NUMPAD2) and BitAnd(1,$tRIK.Flags) Then MyAction()
       Case $VK_NUMPAD2
         If _WinAPI_GetAsyncKeyState($VK_NUMPAD0) and BitAnd(1,$tRIK.Flags) Then MyAction()
     EndSwitch
EndFunc

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam)
        #forceref $iMsg, $wParam
     Switch $hWnd
       Case $hWndKeybd
            Local $tRIK = DllStructCreate($tagRAWINPUTKEYBOARD)
            If _WinAPI_GetRawInputData($lParam, $tRIK, DllStructGetSize($tRIK), $RID_INPUT) Then KeystrokeLogic($tRIK)
     EndSwitch
     If $wParam Then Return 0
EndFunc

 

I will have to figure out how to implement this in the other code, since I've never done this before, but thanks so far. Will let you know if it works hopefully in few hours.

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...