AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Listening to a key while executing its hotkey function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
scrambler



Joined: 18 Aug 2009
Posts: 5

PostPosted: Wed Aug 19, 2009 11:09 pm    Post subject: Listening to a key while executing its hotkey function Reply with quote

I want to create a variable speed FastForward with a Hotkey sending Skips. Below test scripts explaining my problem in achieving that goal:

In the script below, when I type 3 it starts sending f at low speed, then when I press 2 I go to speed 2, then again to speed 3, then again to speed 0.

Code:
SetTitleMatchMode, 2
DetectHiddenText, On
#NoEnv
#Persistent
SendMode Input

#IfWinActive Notepad
Stop = 0      
3:: FFW()
2::
Stop := stop + 1
if stop > 3
   stop = 0
return


FFW()
{
Global stop = 1
   Loop 400
   {
   SplashTextoff
   SplashTextOn,200,50,Speed, %stop%
   Send f
   if stop = 0
      break
   if Stop = 1
      Sleep 500
   if stop = 2
      sleep 150
   if stop = 3
      sleep 50
   
   }
}


This is the functionnality I want, except that I would like to do that by sucessive press of the 3 key instead of using the 3 key to start and the 2 key to change speed.
press 3 starts sending f at speed 1
press 3 again, go to speed 2
press 3 again, go to speed 3
press 3 again, stop

Unfortunately, I cannot get that to work, as it seems that as long as the loop (trigerred by the 3 press) is running, It does not interpret furher key presses of 3.
I tried this without sucess:

Code:
SetTitleMatchMode, 2
DetectHiddenText, On
#NoEnv
#Persistent
SendMode Input

#IfWinActive Notepad
Stop = 0      
3::
Stop := stop + 1
if stop > 3
   stop = 0
FFW()

2::
stop = 0

return


FFW()
{
Global stop
   Loop 400
   {
   SplashTextoff
   SplashTextOn,200,50,Speed, %stop%
   Send f
   if stop = 0
      break
   if Stop = 1
      Sleep 500
   if stop = 2
      sleep 150
   if stop = 3
      sleep 50
   
   }
}

I also tried the script below using getkeystate, but it is unreliable as speed increases (short sleep time).
I believe because getkeystate does not listen all the time for the key press, but only during the brief instruction execution which rarely coincide with the press.
It would equire the press to be buffered, and the buffer to be read by the getkeypress.

Unreliable script:

Code:
SetTitleMatchMode, 2
DetectHiddenText, On
#NoEnv
#Persistent
SendMode Input


#IfWinActive Notepad
Stop = 0
3:: FFW()
2:: Stop = 0
return


FFW()
{
Global Stop = 1
loop 400
   {
       Send f
   SplashTextoff
   SplashTextOn,200,50,Speed, %stop%
   if stop = 0
      break
   if Stop = 1
      Sleep 500
   if stop = 2
      sleep 150
   if stop = 3
      sleep 50
   if getkeystate("3", "p")
      Stop := stop + 1
   if stop > 3
      stop = 0
   }
}

I did succedd to get an acceptable working script with a loop on getkeystate and playing with timing, but it is still sensitive to how long you press the key.
So it would be better if we could use the trigger of the key rather than it state.

Somewhat working script:

Code:
SetTitleMatchMode, 2
DetectHiddenText, On
#NoEnv
#Persistent
SendMode Input


#IfWinActive Notepad
Stop = 0
3:: FFW()
2:: Stop = 0
return

FFW()
{
Global Stop = 1
Loop 400
   {
       Send f
   if stop = 0
      break
   if Stop = 1
   {
      Sleep 250
      loop 2
      {
      if getkeystate("3", "p")
         Stop := stop + 1
      if stop > 3
      stop = 0
      sleep 200
      SplashTextOn,200,50,Speed, %stop%
      }
   }
   if stop = 2
      loop 1
      {
      if getkeystate("3", "p")
         Stop := stop + 1
      if stop > 3
      stop = 0
      sleep 200
      SplashTextOn,200,50,Speed, %stop%
      }
   if stop = 3
      {
      if getkeystate("3", "p")
         Stop := stop + 1
      if stop > 3
         stop = 0
      sleep 50
      SplashTextOn,200,50,Speed, %stop%
      }
   }
}

Welcoming any suggestions Smile
Code:
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 722
Location: Iowa, USA

PostPosted: Wed Aug 19, 2009 11:42 pm    Post subject: Reply with quote

scrambler wrote:
press 3 starts sending f at speed 1
press 3 again, go to speed 2
press 3 again, go to speed 3
press 3 again, stop

Code:
3::
If (!x || x = "off")
   x = 500
Else If (x = 500)
   x = 150
Else If (x = 150)
   x = 50
Else
   x = off
SetTimer, Label, %x%
Return

Label:
Send, f
Return


Or, even better Wink
Code:
3:: SetTimer, Label, % x := !x || x = "off" ? 500 : (x = 500 ? 150 : (x = 150 ? 50 : "off"))

Label:
Send, f
Return

_________________
AHKL, COM_L,Webpage Controls,Donate to AHK


Last edited by jethrow on Wed Aug 19, 2009 11:53 pm; edited 2 times in total
Back to top
View user's profile Send private message
randallf



Joined: 06 Jul 2009
Posts: 394

PostPosted: Wed Aug 19, 2009 11:43 pm    Post subject: Reply with quote

oh, something like, (untested!) fixed slightly
Code:

3::
SetTimer, SendF, off


If Speed = 5 ; if speed is already at 5, disable entirely
{
   Speed = 0
}

If Speed < 5
{
     If Speed <> 0
     {
   Speed++
     }
}


If Speed = 1
{
Send, f
SetTimer, SendF, 4000 ; 4 seconds apart
}

If Speed = 2
{
Send, f
SetTimer, SendF, 2000 ;2 seconds apart
}

If Speed = 3
{
Send, f
SetTimer, SendF, 1000 ;1 second apart
}

If Speed = 4
{
Send, f
SetTimer, SendF, 500 ;half second apart
}

If Speed = 5
{
Send, f
SetTimer, SendF, 250 ;quarter second apart
}

SendF:
Send, f
Return
Back to top
View user's profile Send private message
scrambler



Joined: 18 Aug 2009
Posts: 5

PostPosted: Thu Aug 20, 2009 12:59 am    Post subject: Reply with quote

Thank you both for the quick response.

Two questions:
1) if I also want to be able to interrupt the sending of f by using the 2 key, what would be the syntax? (I tried to add 2:: x = off or 2:: speed = 0) but it does not seem to affect it
2) when x reaches off state (or speed 0), does the loop stops, or does it keep running with the timmer at off.


Thank you again for the help
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 722
Location: Iowa, USA

PostPosted: Thu Aug 20, 2009 1:29 am    Post subject: Reply with quote

Two answers:
1)
Code:
2:: SetTimer, Label, % x := "off"

2) In the example I gave, there isn't a Loop Wink
_________________
AHKL, COM_L,Webpage Controls,Donate to AHK
Back to top
View user's profile Send private message
randallf



Joined: 06 Jul 2009
Posts: 394

PostPosted: Thu Aug 20, 2009 2:59 am    Post subject: Reply with quote

scrambler wrote:
Thank you both for the quick response.

Two questions:
1) if I also want to be able to interrupt the sending of f by using the 2 key, what would be the syntax? (I tried to add 2:: x = off or 2:: speed = 0) but it does not seem to affect it
2) when x reaches off state (or speed 0), does the loop stops, or does it keep running with the timmer at off.


Thank you again for the help


Code:
2::SetTimer, SendF, off


that's it

if you use that you may or may not want another return here:

Code:
If Speed = 5
{
Send, f
SetTimer, SendF, 250 ;quarter second apart
}

Return ;this is the new return

SendF:
Send, f
Return
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 7698
Location: Germany (but I only speak English)

PostPosted: Thu Aug 20, 2009 6:58 am    Post subject: Reply with quote

RapidHotkey might also help you make it. It automatically sets up different things to happen when you press the same key a different number of times.
_________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me.
Back to top
View user's profile Send private message Visit poster's website
scrambler



Joined: 18 Aug 2009
Posts: 5

PostPosted: Thu Aug 20, 2009 5:16 pm    Post subject: Reply with quote

Thanks for all the help!

Sorry for my last post, I rushed into more questions before looking into Settimer in more details (which made the answers obvious Smile)

Below the final test script that I will use with CTRL+F and CTRL+B to have variable FFW and RWD in media center on video files.

Code:
SetTitleMatchMode, 2
DetectHiddenText, On
#NoEnv
#Persistent
SendMode Input

#IfWinActive Notepad
2::
SetTimer, FWS, % x := "off"
SetTimer, BWS, % y := "off"
return

3::
SetTimer, BWS, % y := "off"
If (!x || x = "off")
   x = 500
Else If (x = 500)
   x = 150
Else If (x = 150)
   x = 50
Else
   x = off
SetTimer, FWS, %x%
return

FWS:
Send, f
Return

1::
SetTimer, FWS, % x := "off"
If (!y || y = "off")
   y = 500
Else If (y = 500)
   y = 150
Else If (y = 150)
   y = 50
Else
   y = off
SetTimer, BWS, %y%
return

BWS:
Send, b
Return
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group