How to pass unwanted hotkey through to application

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
pseakins
Newbie
Posts: 8
Joined: Wed Apr 11, 2007 7:33 am
Location: Bundaberg, Australia

How to pass unwanted hotkey through to application

Post by pseakins » Sun Apr 29, 2007 7:45 am

F9 is a common "do something" hotkey in several apps I run. Ie., in Delphi it "runs" the app being developed, in Zeus editor I use it to copy vertically, etc.

So, I decided to make a macro to perform certain complex functions on the highlighted file when F9 is pressed within windows explorer. Great, I've been working for days on this, finally got it all polished. The last step is to pass the F9 key back to windows if I am not in explorer or if the file is not a suitable candidate.

How do you do that? If I place a statement "Press F9" prior to exiting the macro, MacroScheduler appears to go into a fatal loop, sits there for a while shuddering and then just drops out. Hate to think what just happened to system memory. Otherwise, the keystroke has now been sucked up, it no longer works in my editor or Delphi. I don't want to use a different hotkey for my function, I want it to have different meaning depending on which app I am in.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Apr 30, 2007 2:24 am

Hi pseakins,

Usually I've worked around this by using choosing extra modifier keys so instead of using F9, I'd use say CTRL-ALT-F9 to trigger a macro... but I know what you mean. You want F9 to trigger some special processing to be done by your macro if a certain window has focus but at the same time... if some other window has focus, just "pass through" the F9 to that window.

The OnEvent>KEY_DOWN command can be used to detect F9 being pressed but since there is currently no way to "unload" the event once you've set it up... your macro is not going to be able to use the "Press F9" command as it's just going to get hooked by the event again.

Until Marcus gives us a way to Unload an event dynamically, the only way I can think of to do this is by using two separate scripts that call each other. Give these a try:

[code]
/*
=======================================================================
2007-04-29 18:02:23 Sunday by jpuziano
This is Script 1: F9 Detection
See also Script 2: F9 Pass Through

Written to test an idea - in response to this post:
How to pass unwanted hotkey through to application (t=3752)

Two separate scripts are used so that we can:
- detect when F9 is pressed
- if current window=Notepad, do some special function (type some text)
- however, if current windowNotepad, send F9 (pass through)
=======================================================================
*/

//VK120=F9 Key, modifier 0=no modifier key
OnEvent>KEY_DOWN,VK120,0,USER_HIT_F9

//Hit CONTROL-ALT-F9 to exit, modifier 5=CONTROL + ALT keys must be pressed
OnEvent>KEY_DOWN,VK120,5,USER_WANTS_TO_EXIT

Message>F9 Hotkey Detection and Pass-Through Macro now turned on%CRLF%%CRLF%Press CTRL-ALT-F9 to turn this macro off
Wait>2
CloseWindow>Message*

//When variable Keep_Running=0, we will EXIT
Let>Keep_Running=1

//If variable F9TimeOut=0 pressing F9 will be detected and acted upon
Let>F9TimeOut=0

//Variable will disallow F9TimeOut from equalling zero until a timeout has expired
Let>F9Timer=0

//Variable to adjust F9Timer timeout
Let>F9TimerMax=10

//when Pass_Through=yes, we'll perform a "Pass-Through" i.e.:
// -this macro will end
// -a second macro which start which will:
// -Press F9
// -second macro will end
// -this macro will be restarted
Let>Pass_Through=no

Label>START
//A short wait state will make this macro more responsive
//but too short will make it hog too much CPU... adjust as desired
Wait>0.01
If>%F9TimeOut%=1
Add>F9Timer,1
EndIf
If>%F9Timer%>%F9TimerMax%
Let>F9TimeOut=0
Let>F9Timer=0
EndIf
If>Pass_Through=yes,PERFORM_A_PASS_THROUGH
If>Keep_Running=0,EXIT
Goto>START

SRT>USER_HIT_F9
If>F9TimeOut=1,SKIP_THIS_ONE
//get the window_title of the window that currently has focus
GetActiveWindow>window_title,X,Y
//is this window a Notepad window?
Position>Notepad,window_title,1,result
IF>result=0
Let>Pass_Through=yes
ELSE
Send>%CR%"F9 Detection" Macro has detected a Notepad Window - here's some text...
ENDIF
Let>F9TimeOut=1
Label>SKIP_THIS_ONE
Let>F9Timer=0
END>USER_HIT_F9

SRT>USER_WANTS_TO_EXIT
Let>Keep_Running=0
END>USER_WANTS_TO_EXIT

Label>PERFORM_A_PASS_THROUGH
Macro>%SCRIPT_DIR%\F9 Pass Through.scp
Goto>THE_END

Label>EXIT
Message>F9 Hotkey Detection and Pass-Through Macro now turned off
Wait>2
CloseWindow>Message*

Label>THE_END
[/code]

And here's the second script:

[code]
/*
=======================================================================
2007-04-29 18:02:23 Sunday by jpuziano
This is Script 2: F9 Pass Through
See also Script 1: F9 Detection

Written to test an idea - in response to this post:
How to pass unwanted hotkey through to application (t=3752)

Two separate scripts are used so that we can:
- detect when F9 is pressed
- if current window=Notepad, do some special function (type some text)
- however, if current windowNotepad, send F9 (pass through)
=======================================================================
*/

//Wait 1 second to make sure "F9 Detection" which just called this script
//has had time to end and the F9 OnKeyEvent has had time to unload
Wait>1

//This F9 will be detected by the window currently focused
Press F9

Wait>1

//Now we again start up Script 1: F9 Detection
Macro>%SCRIPT_DIR%\F9 Detection.scp

//and this scripts ends...
[/code]

To test this:

- copy/paste to create scripts as above
- if you want, assign a hotkey for the 1st script inside Macro Scheduler, I used CTRL-F9
- now launch the first macro "F9 Detection", just tap CTRL-F9 if you've assigned that as a Hot Key
- the macro briefly displays a dialog telling you its running and to hit CTRL-ALT-F9 if you want to exit
- open a Notepad window
- open an app that responds to F9, I opened UEStudio '06 which opens a DOS Command dialog when you hit F9
- navigate back to Notepad and press F9, you'll see it does the "special processing" which in this case is just typing some text into Notepad
- now navigate back to the other app, for me that's UEStudio '06. I hit F9 and it realizes this is not Notepad and passes F9 to the app and the app responds
- navigate back to Notepad, tap F9, special processing still working
- tap F9 repeatedly and it keeps working

pseakins, you should be able to use this concept to finish the macro you've started... please let us know how it works out for you.

Note: Part of this macro was based on an earlier macro I had posted to the Scripts and Tips forum called: [url=http://www.mjtnet.com/forum/viewtopic.php?t=3754]Assign ESCape key to hotkey[/url]

Because of the extra time delay processing added by JRL to that script, you'll notice that if you just press and hold down F9 indefinitely, you'll get just one "press" of F9, an added bonus.

I invite JRL and others to try out the two macros above and let me know if they are stable and work for you or if anyone can suggest improvements.

I haven't tested to see how the above two macros would perform if compiled and run as EXE files. Some extra modifications may be necessary. On a really old PC, the time it takes to load a compiled script may slow things down; I think response time is going to be better just running these as scripts on a PC with Macro Scheduler installed. I'd be curious to know though so if anyone tries compiled versions, please let us know how it works for you.

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Apr 30, 2007 6:37 am

Hotkeys in Macro Scheduler are system wide. Once set up, they are exclusive. Most actions have multiple ways of triggering them. F9 in Delphi does the same as ALT+r+r. So our hotkey macro can do:

Code: Select all

GetActiveWindow>title,X,Y
Pos>delphi,title,1,isdelphi
If>isdelphi>0
  Press ALT
  Send>rr
  Release>ALT
Endif
At present there is no way to unload a Macro Scheduler hotkey and therefore release it from it's macro assignment. But this might be a candidate for future functionality. In the mean time I would advise to not assign hot keys that are also used elsewhere. I.e. if you use F9 in Delphi, use CTRL+F9 for your macro. Or find an alternative "pass-thru" like the above example.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts