Jump to content

Recommended Posts

Posted (edited)

Hi all

There are situations where you registered a Hotkey for a certain key,

and you want to send that key to the currently running program.

 

What is the best way to do it?

 

Currently, the what I do is Unset, Send, then Re-set:

Func    SendARegisteredHotKey($HotKey,$HandlerFunction_Name)        ;This Functions Sends a Registered HotKey via Doing:  Unset+Send+Set
    HotKeySet($HotKey)                          ;Unsetting, so I can Send it
    Send     ($HotKey)                          ;Sending
    HotKeySet($HotKey,$HandlerFunction_Name)    ;Setting it Back
EndFunc

 

Is there maybe a better way than Unsetting and then Re-setting the Hotkey every time we need to send it?

 

Thank you

Edited by Zohar
Posted

Depends on what you actually want to accomplish.  That is one, but there is multiple ways to achieve something similar.  You should provide a more complete example or a better explanation, so we can suggest an appropriate solution.

Posted (edited)
4 hours ago, Nine said:

That is one, but there is multiple ways to achieve something similar.

You should provide a more complete example or a better explanation, so we can suggest an appropriate solution.

Hi Nine

Sure, I'll give you my exact issue.

 

I set a Hotkey for the F9 key (and some other keys too),

and indeed I use this all accross Windows, and it all works good.

 

However, there are some specific programs, inwhich I don't want to capture this key (e.g. F9),

and instead I want to pass the key to the program, so I can work with the program in a normal way,

and not have some keys that are not pressable for that program, because they are taken by the hotkeys I set.

 

An example of a program like that is PuTTY - a well known Terminal Program:

In PuTTY for example I do need the F9 key to be sent to it when I press it,

because there are many programs in the terminal that use F9 for various operations.

(example: Midnight Commander has a bottom menu, with F1..F10 keys)

 

So what I do in my AutoIt code is I identify the currently active window,

and if it's one of the windows in my "Pass List" (which PuTTY is one such window),

then I want to pass the hotkey to that program, instead of handling it and masking it from the program.

 

As I wrote in the first post, the solution of Unset+Send+Re-set works,

but I don't know if Unsetting and Setting the hotkey every time I press it is the best solution.

 

That's why I asked it here..

Edited by Zohar
Posted

Based on what you described, I think it is the best solution you are taken (unless there is issue with it).  One other solution would be to hook (system wide) your keyboard and analyse what is the current app and act upon it.  But it would make the script way more complex, although faster.  If speed is not a problem, I would suggest you stay with your approach.

  • Zohar changed the title to [Closed] Sending a Key that is Registered as a Hotkey
Posted
3 hours ago, Nine said:

Based on what you described, I think it is the best solution.

If speed is not a problem, I would suggest you stay with your approach.

Thank you very much Nine.

I will stay with that solution then.

Posted

my two cents, You could use WinActive() + ControlSend() to send that key to a specific window only if that window is the active one, otherwise stick with the "global" HotKey functionality

Global $HotKey = '{F5}'
HotKeySet("{ESC}", "Terminate")
HotKeySet($HotKey, "MyHotKey")

    ; Run Notepad for example
     Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd1 = WinWait("[CLASS:Notepad]", "", 10)

While 1
        Sleep(100)
WEnd

Func MyHotKey()

    ; Exception for HotKey
    ; Perform following action(s) only if the chosen window is the active window
    If WinActive($hWnd1) Then ; do this only if Notepad is the active window
        ; send HotKey to a specific window
        ControlSend($hWnd1,'','', $HotKey)
        Return
    EndIf

    ; otherwise do standard global action
    ConsoleWrite($HotKey & " was pessed (outside the notepad)" & @CRLF)
EndFunc

Func Terminate()
        ; Close the Notepad window using the handle returned by WinWait.
        WinClose($hWnd1)

        ; Now a screen will pop up and ask to save the changes, the classname of the window is called
        ; "#32770" and simulating the "TAB" key to move to the second button in which the "ENTER" is simulated to not "save the file"
        WinWaitActive("[CLASS:#32770]")
        Sleep(500)
        Send("{TAB}{ENTER}")
        Exit
EndFunc   ;==>Terminate

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (edited)

Hi Gianni

Your idea is fantastic.

Using

ControlSend("","","",$HotKey)

indeed does not Trigger the registered HotKey, and instead successfully passes it to the current window.

 

Thank you so much for it,

I am using it now as the better method for achieving this.

 

BTW, you wrote that it works only fr current window,

but out of curiosity I tried also to send it to a non-active window,

and it works well there too.

 

This is a great way to send a registered HotKey to the current program,

without needing to Unset+Re-set it.

Edited by Zohar
  • Zohar changed the title to [Solved] Sending a Key that is Registered as a Hotkey

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...