Zohar Posted Wednesday at 07:28 AM Posted Wednesday at 07:28 AM (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 Thursday at 09:00 AM by Zohar
Nine Posted Wednesday at 12:12 PM Posted Wednesday at 12:12 PM 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
argumentum Posted Wednesday at 01:53 PM Posted Wednesday at 01:53 PM 6 hours ago, Zohar said: ...and you want to send that key to the currently running program. GUISetAccelerators ( accelerators [, winhandle] ) ; Sets the accelerator table to be used in a GUI window. Maybe that's what you're looking for ? Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Zohar Posted Wednesday at 03:11 PM Author Posted Wednesday at 03:11 PM (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 Wednesday at 03:24 PM by Zohar
Nine Posted Wednesday at 04:01 PM Posted Wednesday at 04:01 PM 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Zohar Posted Wednesday at 06:04 PM Author Posted Wednesday at 06:04 PM 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.
Gianni Posted Thursday at 01:32 AM Posted Thursday at 01:32 AM 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 expandcollapse popupGlobal $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 Zohar and argumentum 1 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Zohar Posted Thursday at 08:59 AM Author Posted Thursday at 08:59 AM (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 Thursday at 09:01 AM by Zohar
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now