0

How can I make a hotkey be specific to an application, but conditional to some factor so that if the condition is not true the hotkey will be handled by the native/default handler?

I've made a hotkey that I want to works in MS Access when I'm in query design mode. I am detecting this mode using the FindText image search. If the mode is detected, I want my autohotkey script to run and not pass back the keystroke to Windows or Access. If the mode is not detected, I want Windows/Access to be able to handle the keystroke.

I don't know how to get the script to ignore that hotkey if the condition isn't detected. I have a script like this:

#IfWinActive ahk_class OMain ; Access
^R::  ; CTRL+R

; Use FindText to see if a certain image is on screen
If FindText()...
{
     ; do my automation of Access
}
else
{
     ; I'm not in the design mode.  Let the CTRL+R key be handled by Access or Windows.
}
return
#IfWinActive 

I tried to prefix the hotkey with the tilde, but that would allow Access to do something I don't want to happen in design mode.

What are some ways I can handle this?


For the curious, I have created an AutoHotkey macro for Access that will expand all of the columns widths in the Query Design view. This saves my sanity when trying to review long formulas.

One keystoke changes this: before

To this:

after

2

You can just use SendInput to do what you want here.

A lot of my own keys are setup that way. Capture some keystroke, check a condition and handle it or let the keys "pass through" using SendInput

For example F11 on the browser: if the title is such-and-such, I do certain things like fill-in a form, but otherwise, I use SendInput {F11} to "pass-through" to the browser to do its normal thing of full screening.

#IfWinActive ahk_class OMain ; Access
^R::  ; CTRL+R

; Use FindText to see if a certain image is on screen
If FindText()...
{
     ; do my automation of Access
}
else
{
     SendInput ^r    ;this is all you were missing
}
return
#IfWinActive 
  • I haven't been successful at getting this to work. Is there another method I can try? – Ben Aug 15 at 4:24
  • Let's test by remapping F1. Put each numbered item on its own line without the number 1. F1:: 2. SendInput ^r 3.Return Does pressing F1, send a Ctrl+R? – Kevin McCabe 2 days ago
  • I got it working just as you described. For anyone else reading this please note that you need to use SendInput with a lowercase r because sending uppercase R effectively sends Shift+r. This was confusing to me because my hotkey begins with ^R but isn't listening for a shift. – Ben 2 days ago
  • Good info Ben. Not sure I would have caught that myself! – Kevin McCabe 11 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.