Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

WaitForAnyKey


  • Please log in to reply
14 replies to this topic
shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005
Self-explanatory.

#InstallKeybdHook

if ( WaitForAnyKey( 10000 ) )
	MsgBox, <any key> pressed
else
	MsgBox, [WaitForAnyKey] timeout expired
ExitApp

WaitForAnyKey( p_timeout )
{
	start := A_TickCount

	loop,
	{
		if ( idle > A_TimeIdlePhysical )
			return, true
		else if ( A_TickCount-start >= p_timeout )
			return, false
	
		idle := A_TimeIdlePhysical

		Sleep, 10
	}
}


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Creative use of A_TimeIdlePhysical. I'm going to try to remember to refer people to this topic when they ask about "wait for any key" (though I'm not sure the question comes up quite often enough to go into the FAQ).

shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005

Creative use of A_TimeIdlePhysical


Thanks.

I thought, there sits a variable idle and what with it to do?

Some time passed and I waited patiently. Until I realized, that WaitForAnyKey was ripe.

Then there it was, and here it is.

garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
where is the key "any" ? :)
sorry shimanov (russian?) , very nice...

shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005

where is the key "any" ? :)


Funny. Maybe it should be
one, some, every, or all without specification key.But any key by any other name is still a key.

TheLeO
  • Members
  • 264 posts
  • Last active: Jan 02 2012 01:51 AM
  • Joined: 11 Jun 2005
mm.. doesnt register mouse events, nor its movments or mouse clicks.,, so,, i could be using the computer with the mouse and it wouldnt notice..
wonder if that is a bug..

any ways, this is a Sub that i use if i want to wait witht the thread untill the mouse is moved ( i use to get out of a home made screensaver) :

Wait4MouseMovement:
MouseGetPos,pos1,pos2
last1 = %pos1%
last2 = %pos2%
loop
{
  GetKeyState, RBS, RButton, P ; rbs= Right button state
  GetKeystate, MBS, MButton, p ; Middle...
  GetKeystate, LBS, LButton, p ; Left... 
  GetKeystate, X1BS, XButton1, p ; my 4th mouse button
  GetKeystate, X2BS, XButton2, p ; mt 5th button.
  ButtonState = %RBS%%MBS%%LBS%%X1BS%%X2BS% ;Puts all states in one
  if ButtonState <> UUUUU ; "<>" means  "If not" , so if they are not all up.
    Break
  MouseGetPos,pos1,pos2
  if pos1 <> %last1%
    break
  if pos2 <> %last2%
    break
}
Return

perhaps we should merge them. :wink:

[EDIT] :!:
Solved:

Screw my script, Just add :
#InstallMouseHook
to the autoexecution bit and it will do it for the mouse as well.
::
I Have Spoken
::

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Something like this:?
if ( Wait4MouseAction( 10000 ) ) 

   MsgBox, <any mouse action> detected 

else 

   MsgBox, [Wait4MouseAction] timeout expired 

ExitApp 



Wait4MouseAction(p_timeout )

  {

    start := A_TickCount 

    MouseGetPos,last1,last2 

    Loop 

      { 

        GetKeyState, RBS, RButton, P

        GetKeystate, MBS, MButton, p

        GetKeystate, LBS, LButton, p

        GetKeystate, X1BS, XButton1, p

        GetKeystate, X2BS, XButton2, p

        BtnState = %RBS%%MBS%%LBS%%X1BS%%X2BS%

        MouseGetPos,pos1,pos2 

        if ( BtnState <> "UUUUU" Or pos1 <> last1 OR pos2 <> last2)

            return, true

        if ( A_TickCount-start >= p_timeout ) 

            return, false 

        Sleep, 10 

      } 

  }
*not tested* Just LEO code tweaked and adjusted to shimanovs code.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005

Screw my script, Just add :
#InstallMouseHook
to the autoexecution bit and it will do it for the mouse as well.

If so, disregard my post.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005

doesnt register mouse events...

#InstallMouseHook


You found the solution. But, technically, it is WaitForAnyKey.

I have heard of those who would type with mouse buttons and gestures -- weird stuff.

Tekl
  • Members
  • 814 posts
  • Last active: May 03 2009 03:28 PM
  • Joined: 24 Sep 2004
Hi,

is there a way to temporary disable the mousehook or to ignore mouse-movements?
Tekl

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
The mouse hook can be dynamically deactivated by:
1) Avoiding #InstallMouseHook.
2) Specifying #Hotstring NoMouse (if your script contains hotstrings). Or dynamically disabling all hotstrings via the Suspend command.
3) Dynamically disabling all mouse hotkeys. In other words, the mouse hook is activated only when the program determines that something in the script (usually hotkeys) requires it. If nothing requires it, the hook is deactivated.

lazy wiiox
  • Guests
  • Last active:
  • Joined: --
Is there a way to disregard certain keys?

Say I want my script to stop, when any key other than, say a defined hotkey is pressed. How would I define that?

Timm
  • Guests
  • Last active:
  • Joined: --
Please, could help me how can I do: Press key B and then WaitForAnyKey?

When I try this:
$b::
if (WaitForAnyKey(10000))
{
...
}
return

When I press B, WaitForAnyKey return true without waiting for next key?

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
because it starts waiting before you release the b key. the action of releasing the b key counts enough for this code as a key action.. you could add a short sleep to allow for you to release the button, or you can use keywait to wait until you release the button.

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
It seems to me that this function takes advantage of an undocumented feature: A_TimeIdlePhysical can be made to ignore physical input of one type or the other by installing only one hook. In talking with Chris, it would be considered a bug since both A_TimeIdle and A_TimeIdlePhysical are intended to always capture both types of physical input (keyboard and mouse); however, I think it's a great feature and should simply be added to the documentation.

The script seems to have worked as advertised for everyone else in this thread (three years ago) and it works the same for me:

1. Installing only the keyboard hook ignores physical input from the mouse.
2. Installing only the mouse hook ignores physical input from the keyboard.
3. Installing both hooks or installing neither captures all input.
4. Using A_TimeIdle instead of A_TimeIdlePhysical captures all input regardless of the hooks installed.

Anyone care to try?
;#InstallMouseHook
#InstallKeybdHook
While A_TimeIdlePhysical > 10
 continue
MsgBox, A_TimeIdlePhysical has been reset.
ExitApp

EDIT: The documentation has been updated.