What is the most correct way to run a program? It's not so obvious!

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 174
Joined: 05 May 2017, 13:19

What is the most correct way to run a program? It's not so obvious!

14 Sep 2018, 20:42

The question seems to be very easy, but it's not so obvious.

For example, try to press Win+1 without AutoHotkey. What will you see? As soon as you pressed it, the first program from taskbar will be launched.

And here is simple script for it:

Code: [Select all] [Download] GeSHi © Codebox Plus

; This:
F1:: SendInput, #1
; Or this:
F1:: #1


But with script, the behavior is different. If you keep F1 pressed, instead of quickly release it, the application will be launched multiple times.

Here is another way, with KeyWait command:



It will fix the issue with multiple launches, but introduces another issue - the program will be launched when F1 released, instead of pressed. So, the behavior is different from default Windows behavior (still).

Probably there exists some another, more robust way of launching programs?

I don't mean launching from taskbar, though. It was just an example. Here is another example, which is more close to real life:

Code: [Select all] [Download] GeSHi © Codebox Plus

F1:: Run, C:\foo\bar\some_program.exe
Last edited by john_c on 16 Sep 2018, 07:11, edited 1 time in total.
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: What is the most correct way to run a program? It's not so obvious!

14 Sep 2018, 21:56

The correct way to run a program is to run the program, as you've done at the end of your post.

Your problem has very little to do with running the program. Your problem is that you expect the hotkey's action to execute only once each time you press and release the key, which does not align with how keyboard keys work. If you don't want a key's function to repeat while you hold it down, you can turn off key-repeat. Otherwise, you should expect this behaviour and work around it on a case-by-case basis.

Most likely the solution you're after is to simply put KeyWait, F1 after SendInput, #1.

But with script, the behavior is different. If you keep F1 pressed, instead of quickly release it, the application will be launched multiple times.
Different to what? It is consistent with standard Windows 10 behaviour. If I hold #1, it will repeatedly launch that application for each time the key repeats.
john_c
Posts: 174
Joined: 05 May 2017, 13:19

Re: What is the most correct way to run a program? It's not so obvious!

15 Sep 2018, 05:26

Most likely the solution you're after is to simply put KeyWait, F1 after SendInput, #1.


Thanks! Yes, it seems to work correctly! Isn't it a best practice to always use KeyWait before last Return?

Different to what? It is consistent with standard Windows 10 behaviour. If I hold #1, it will repeatedly launch that application for each time the key repeats.


Hm. I use Win7. In Win 7, the application will be launched each time you press Win+1. But if you press it only once and then continue keep your fingers on buttons, the application will not be launched repeatedly - it will be launched just once. With AutoHotkey, if we keep fingers on buttons for some time (e.g. 5 seconds), the application will be launched again and again (if we don't use KeyWait).
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: What is the most correct way to run a program? It's not so obvious!

15 Sep 2018, 17:56

john_c wrote:Isn't it a best practice to always use KeyWait before last Return?
It isn't "best practice". It is common sense. If you put a command after return, the command will not execute, because the subroutine has already returned.

Otherwise, you put KeyWait at whatever point you want the script to wait. Whether you do so before or after performing some other action has no impact on whether key-repeat triggers the hotkey. Key-repeats are ignored because the hotkey hasn't returned yet, which is because it is still waiting.
DRocks
Posts: 89
Joined: 08 May 2018, 10:20

Re: What is the most correct way to run a program? It's not so obvious!

15 Sep 2018, 20:41

Interesting!

Then what is the difference, please, with using something like F11 UP:: ?

Would F11 UP:: be too drastic compared to F11:: KeyWait F11... ?

Thanks in advance.

Edit: I am thinking about this with a context in the back of my head: im trying with Wolf_II to script the best possible Edit control that only takes Numbers, 1 dot, two decimals, etc. We found that interception of keys before they are typed is better than String manipulations.

So,
To limit key repeat im using the key UP option... and then also a inStr check to valide presence or absence of a Dot in the edit control. If there is one it return rather than trigerring on key UP.

Do you think that this is valid and logical or that KeyWait is more appropriate?
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: What is the most correct way to run a program? It's not so obvious!

15 Sep 2018, 23:36

john_c wrote:Isn't it a best practice to always use KeyWait before last Return?
I realize I may have misinterpreted the question; that the emphasis may have been on always using KeyWait, and not on where to place it ("before last Return"). It is not best practice to block key-repeat. However, if you want to do it, KeyWait is the easiest way.

DRocks wrote:Then what is the difference, please, with using something like F11 UP:: ?
Putting KeyWait before the action, as john_c did, has a similar effect to a key-up hotkey. If you had paid attention to the topic, you would see that this effect was not desired.
john_c wrote:introduces another issue - the program will be launched when F1 released, instead of pressed.

There's nothing "drastic" about an up-hotkey. If you want to perform some action only when the key is released, it is the best way. john_c did not want to perform some action when the key was released, so an up-hotkey was not appropriate. As for your own script, that is a separate topic.

Off-topic: If you are creating the Edit control, it is not appropriate to use hotkeys, which rely on global registration or a system-wide hook. Use OnMessage to monitor WM_KEYDOWN and related messages.
john_c
Posts: 174
Joined: 05 May 2017, 13:19

Re: What is the most correct way to run a program? It's not so obvious!

16 Sep 2018, 06:34

DRocks wrote:Then what is the difference, please, with using something like F11 UP:: ? Do you think that this is valid and logical or that KeyWait is more appropriate?


Hi! I'm just newbie.

Notice the difference:

1st example: SendInput will be launched when you RELEASE the button. It's NOT how Windows works by default:



2nd example: SendInput will be launched when you PRESS the button. This is exactly how Windows works by default. So, from UX-centric point of view it seems to be better:

Last edited by john_c on 16 Sep 2018, 06:41, edited 4 times in total.
john_c
Posts: 174
Joined: 05 May 2017, 13:19

Re: What is the most correct way to run a program? It's not so obvious!

16 Sep 2018, 06:37

lexikos wrote:I realize I may have misinterpreted the question; that the emphasis may have been on always using KeyWait, and not on where to place it ("before last Return"). It is not best practice to block key-repeat. However, if you want to do it, KeyWait is the easiest way.


Thanks! Yes, it was exactly what I asked for! :)
DRocks
Posts: 89
Joined: 08 May 2018, 10:20

Re: What is the most correct way to run a program? It's not so obvious!

16 Sep 2018, 07:20

Thanks to both of you! I get it much better now and am able to make more sense out of it in my mind.

And tha ks for the nice tip Lexikos about the OnMessage being more appropriate. I assume when you interpret "create" the edit control we are both talking about making a Gui, add, edit,.... control right? This is what I meant when I said "create".

Ill investigate on the hint you gave me. Very interesting. Thanks

Édit: ive Made a function Edit OnMessage to do basic stuff so far so good... but its too complex versus AHK building hotkey feature. Il using the #if focused control match Edit_ to only affect keyboard in this context. Is this proper too according to how AHK is made?

Return to “Ask For Help”

Who is online

Users browsing this forum: Zumwalt and 67 guests