Jump to content

Need help on HotKeySet function


Recommended Posts

Hello all!

Here is my little try with AI3

Opt("WinTitleMatchMode", 4)

HotKeySet("{ENTER}", "_Enter")

Run("cm.exe")

While 1

Sleep(1000)

WEnd

Func _Enter()

If WinActive("classname=obj_Form") Then

....

; do something

...

EndIf

EndFunc

When cm.exe is in front, it works fine as I design.

But if switch to other window, "ENTER" key doesn't work there. It's captured by the script, I guess. And If statment doesn't avoid that.

What can I do to make Enter key do as I design when cm.exe window is active, and works as it is when other windows are active?

Edited by Raymond
Link to comment
Share on other sites

Personally I would not use the enter key for a hotkey. I suggest using one of the function keys or unsetting the hotkey inside the function.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Tested with the AutoIt Helpfile.

Global $hotkey, $title = "AutoIt Help" ;"classname=obj_Form"
Opt("WinTitleMatchMode", 4)
;Run("cm.exe")

While 1
    If WinActive($title) And Not $hotkey Then
        $hotkey = HotKeySet("{ENTER}", "_Enter")
    ElseIf Not WinActive($title) And $hotkey Then
        HotKeySet('{ENTER}')
        $hotkey = 0
    EndIf
    Sleep(1000)
WEnd

Func _Enter()
    ConsoleWrite('HotKey is On' & @LF)
EndFunc

:D

Link to comment
Share on other sites

Personally I would not use the enter key for a hotkey. I suggest using one of the function keys or unsetting the hotkey inside the function.

Yes I can change the hotkey.

But I want to know how to make HotKeySet function only apply to specific window, keeping others as they always are.

I bet AI3 can do that, only I don't know yet.

Edited by Raymond
Link to comment
Share on other sites

Yes I can change the hotkey.

But I want to know how to make HotKeySet function only apply to specific window, keeping others as they always are.

I bet AI3 can do that, only I don't know yet.

If WinActive('The window you want the hotkey for') Then

     HotKeySet('{ENTER}', "_Enter")

Else

     HotKeySet('{ENTER}') ; cancels hotkey

EndIf

Nomad :D

Edited by Nomad
Link to comment
Share on other sites

  • Moderators

Yeah! This does the trick.

Thanks for your help.

Only still don't get why If statement (as first floor) doesn't work as it should be.

Because you only want the Hotkey to effect "your active" window, not the others as you stated. The hotkey is activated when the window is active and deactivated when it isn't.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Because you only want the Hotkey to effect "your active" window, not the others as you stated. The hotkey is activated when the window is active and deactivated when it isn't.

In my first post my code is like

Func _Enter()

If WinActive("classname=obj_Form") Then

....

; do something

...

EndIf

EndFunc

It seems to me not big difference from Nomad's code. But only his one works.

I wonder why.

Link to comment
Share on other sites

Yeah! This does the trick.

Thanks for your help.

No problem, just put that into a function and call the function anytime the window changes and it will enable/disable the hotkey as needed. There are other methods you could also do, but this is probably the simplest method.

As far as the hotkey functioning/not functioning... a hotkey cannot be assigned by AutoIt if it is already assigned by another program. Likewise, if the hotkey is already reserved by your script, it probably cannot be assigned by another program.

There are ways around this though, as has been proven by the "full screen" mode of some games. I've started scripts before starting a full screen game, and the game automatically blocked my hotkeys and wouldn't allow them to function while in full screen. But the game hotkeys, even when the keys were the same as in my script, still functioned. So, there is a way, but I don't know it, yet. :D

Nomad :D

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...