AutoHotkey Community

It is currently May 27th, 2012, 5:44 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: March 16th, 2010, 2:44 pm 
Offline

Joined: March 13th, 2010, 10:10 pm
Posts: 4
Hi,

I am writing an AHK program that will basically use Windows Media Player to play a DVD for an certain number of seconds (variable) then pause the player, wait for and "ENTER" input from the user and then resume the DVD... again for the requested number of seconds. I have made some progress (new to any programming) but now I need some advice please.

Here's what I have accomplished

- AHK will start and ask for the number of seconds the therapist wishes it to run (%secs%)
- If Windows Media Player is not active it will start it
- It will wait for the DVD to be manually started and for the therapist to click "OK"
************************
HERE'S WHERE I AM STUCK - THIS IS WHAT I NEED FROM HERE ...

I would like AHK to enter a loop (?) and count up to the number of %secs% - then send a pause command to Windows Media Player (CONTROL+P) - then wait for an "ENTER" input and then send a run command to WMP (CONTROL+P) - then start counting the seconds again up to the number of %secs% specified - send a pause ---- etc etc etc

To end the program at anytime I need to add a line that would make ESCAPE (?) quit AHK & WMP

I am very appreciative for any help I can get.

Thank you,

Mike


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 2:59 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
No need for the loop, use

Sleep

--------------------------------------------------------------------------------

Waits the specified amount of time before continuing.

Sleep, DelayInMilliseconds

once the Therapist clicks OK start a timer that points to the pause code. setup a trigger on ENTER to send the run command and another trigger to quit.

It sounds like you are learning a lot, keep asking questions - we ALL love to help.

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 9:03 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
Hello Mike0627,

My M.O. is usually to help to bring other members along by giving pointers as to where to find the solution,
so that they learn "where to find the solution".

>>help with program for disabled son

To me that ranks well above helping someone with another WOW script.

May I offer this script as well-wishes to you, he, and any others near to you. :)
I hope this spurs you on to more Ahk.

I have not tested the WMPlayer aspect, because I don't allow the program on my computer
(it used to query an inserted flash drive every 10-15 seconds :evil: )

Code:
min_play=3
play_time=6   ;<---- time to play DVD in seconds
max_play=10


screen_x=0   ; customize location of window, if desired
screen_y=0   ; see "gui, show" below to enable

OnExit, quit   ;

title=DVD Play Timer
gui, font,s12

gui, add, text, , %title%

gui, font,s10

gui, add, text, center xm , Play Time (secs)
gui, add, edit, w70
Gui, Add, UpDown, vplay_time Range%min_play%-%max_play%, %play_time%

gui, add, button,  yp x+10 vok Default, OK
;gui, add, button,  x30 vok Default, OK
gui, add, text, center xm , Press Ok or Enter `nafter movie started
gui, add, text, center  xm, Press ESC`nto exit at any time

show_gui:
;==================================================================
;    gui position, default of centered on screen
gui, show, w150 h180 ,DVD play timer
;==================================================================
;    gui position, --customized--
;gui, show, w150 h180 x%screen_x% y%screen_y%,DVD play timer


return

buttonOK:   ; the assumption is that the DVD has been started manually prior the button push

gui, submit   ; gets the current sleep time and hides the gui

sleep, play_time * 1000 ; sleep command is based on units of 1/1000 second


send ^p   ; normal way to send control-p

      ; some systems may be stubborn and require alternate control-p               
      ; for now this next line is commented out and will not be run
; send {control down} p {control down} 


goto show_gui   ; show the interface again


GuiClose:
esc::
  exitapp


quit:
; winclose, Untitled      ; close a window with title that starts "Untitled"
; winclose, ahk_class Notepad   ; close a window of class matching notepad
winclose, ahk_class WMPlayerApp ; this may be right for WMP, I dunno.
exitapp

;f12::reload   ; used for development purposes

;f1::         ; to find the window class, run the script, place mouse over window and hit F1
MouseGetPos,,, WinID
WinGetClass, class, ahk_id %WinID%
clipboard=ahk_class %class%
msgbox window class copied to clipboard`nahk_class %class%
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 2:46 am 
Offline

Joined: March 13th, 2010, 10:10 pm
Posts: 4
Thank you both so much. I can't believe how fqast those answers came. THanks also for all the coding work, I am truly thankful for all your effort. I do not understand the GUI stuff, so that is going to be a learning curve if that is the way I need to go.

Here is what I have done so far. Bear with me - this is a few days work for me! You will see my notes in CAPS about 3/4 of the way down. MAybe I am way off base in how I am trying to accomplish this. I am grateful for any help.

I am sorry I saw this reply so late, I will check out the GUI code in the morning. Thanks to all!!

Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance force

; HotKey, Esc, on ; Assign a hotkey to terminate this script.


MsgBox, 4,Bretts DVD timer , This will start and run Bretts DVD timer program`n `nWould you like to continue?, 605  ; 60-second timeout.
   IfMsgBox, No
      ExitApp ; User pressed the "No" button.
      
   InputBox, secs, Set Timer, Enter the number of seconds for the program to run between prompts?, , , 250 , 50 , , , 60 , 60
      time=%secs%*1000
      IfMsgBox Timeout, ExitApp

   
; check to see if player active
   IfWinActive, Windows Media Player
      WinMaximize  ; Maximizes the window found by IfWinActive above.
         Else run "C:\Program Files\Windows Media Player\wmplayer.exe" /open
      Sleep 1000


   IfWinActive, Windows Media Player
      MsgBox, 4145, Do This Now!!, While this window is up insert the DVD into the player and start it playing.`n  `nThe DVD will play for %secs% seconds then pause for input.`n Once the DVD is playing select OK to start the timer. `nHitting "Cancel" will EXIT the program., 120
      IfMsgBox, Cancel
      WinClose, Windows Media Player


*************************************8****************
******************************************************
; HERE IS WHERE MY NEXT CHALLENGE IS - THIS WILL WAIT THE RIGHT NUMBER OF SECONDS,
; AND SEND A SERIES OF COMMANDS THAT WIL PAUSE MEDIA PLAYER - THAT WORKS...
; KEYWAIT DOES NOT WORK... IT JUST SKIPS RIGHT BY IT
; I WANT THE PROGRAM TO PAUSE WHERE THE KEYWAIT IS ... AND WAIT FOR {ENTER}
; THEN RUN THE LOOP AGAIN....
;
; i ALSO NEED TO ASSIGN A HOTKEY SO {escape} WILL QUIT THE APP WHENEVER IT IS PRESSED. 
; I TRIED A LINE FOR THAT UP TOP BUT IT DIDN'T WORK....
      
loop
{
   Sleep, (secs*1000)
   ControlFocus, Windows Media Player
   Send {Alt}
   Send P
   Send {ENTER}
   SoundBeep, ,1500]
      KeyWait, {Enter}
}



Mike


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 3:15 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
Mike, there are a number of ways to accomplish what you are trying to do. It the GUI method above looks like too much for you, using an inputbox to gather the time is just fine.

I would recommend using a timer instead of a loop. Also, your Keywait function should not use the curly brackets to name the key, this is likely the issue. I will leave learning timers to you though, I don't want to give you any code you can't figure out yourself.

When you make a hotkey using the Hotkey command, the second parameter is not on/off, it is the label to use. The esc key does not have a function until you give it one, simply turning it on as a hotkey will not make it exit the program. If you are just trying to make the escape key exit the program, the Hotkey command is probably the wrong way to go about it. You can define hotkeys using a double-colon label directly like this:

Code:
;single line method
ESC::exitapp

;multiple line method (use when the key will execute more than one line of code)
ESC::
msgbox, You pressed the ESC key, the program will not exit.
WinKill, Windows Media Player
exitapp



Here is your script, with some small modifications. I still recommend reviewing the linked documentation. (Like leef-me, I cannot test this due to not having Windows Media Player)


Code:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance force

MsgBox, 4,Bretts DVD timer , This will start and run Bretts DVD timer program`n `nWould you like to continue?, 605  ; 60-second timeout.
   IfMsgBox, No
      ExitApp ; User pressed the "No" button.
     
   InputBox, secs, Set Timer, Enter the number of seconds for the program to run between prompts?, , , 250 , 50 , , , 60 , 60
      time=%secs%*1000
      IfMsgBox Timeout, ExitApp

   
   ; check to see if player active
      IfWinActive, Windows Media Player
         WinMaximize  ; Maximizes the window found by IfWinActive above.
      Else run "C:\Program Files\Windows Media Player\wmplayer.exe" /open
   Sleep 1000


   IfWinActive, Windows Media Player
      MsgBox, 4145, Do This Now!!, While this window is up insert the DVD into the player and start it playing.`n  `nThe DVD will play for %secs% seconds then pause for input.`n Once the DVD is playing select OK to start the timer. `nHitting "Cancel" will EXIT the program., 120
         IfMsgBox, Cancel
            WinClose, Windows Media Player
   
   loop,
      {
      sleep, %time%
      ControlFocus, Windows Media Player
      Send !p
      Send {ENTER} ;I left this in, because I am not sure why you had it there to begin with
      SoundBeep, ,1500
      sleep, 100
      KeyWait, Enter
      }


ESC:: ;This key should exit both the script and Windows media player when pressed
msgbox, You pressed the ESC key, the program will not exit.
WinKill, Windows Media Player
exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 7:52 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
Hi again Mike0627,

Sorry, didn't mean to confuse you with GUI. You didn't post your script and I just got gung-ho on wanting to help.
I intended it as a plug-and-go for you to use, but that was somewhat ill-mannered. :oops:

Looking at your script it appears you know what you want and you aren't doing anything weird, so why no go with yours?

I have 4 suggestions:
1) _After_ your "Do this now!" msgbox, add curly brackets and one command
Code:
IfMsgBox, Cancel
{
      WinClose, Windows Media Player
      exitapp ; <------------------
}


in your loop with sleep and key wait
2) I don't believ that "ControlFocus, Windows Media Player" does anything, try commenting it out to confirm or prove me wrong.

3) Correct the "keywait" line to KeyWait, Enter

4) I agree with Tyrsius's "multiple line method" but not with his advice for using settimer.

Let us know how you fair.
Leef_me


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 12:42 pm 
Offline

Joined: March 13th, 2010, 10:10 pm
Posts: 4
Thanks to all for your help. I think my brain is not wired right to understand this stuff, so I really appreciate all your time.

Here are my latest frustrations:

I need a way to stop the app from running. I was trying to use the escape key... but it doesn't matter what I use, I just need a way to terminte the app & shut it down whenever they want to. I have tried
Code:
; ESC::exitapp

; Hotkey, Escape
;   ExitApp

;multiple line method (use when the key will execute more than one line of code)
;   ESC::
;   msgbox, You pressed the ESC key, the program will not exit.
;   WinKill, Windows Media Player
;   exitapp


They all just exit the app immediately when run... nothing happens but when I comment them out the script starts. So I need to learn what it is I am missing there... Are they positioned wrong? I have them at the start of the script.

#2 - When I manually send (one at a time) an ALT, a "p" and then an "ENTER" to WMP it will pause ... and then run when I send those same keystroke again. So I know that those keys can work. Actually so will a CONTROL+P - but I can't get that to work either

What I need is for the script to wait for an "ENTER" between those two instances. The timer runs - sends ALT, P, ENTER (to pause) -- then wait for an "ENTER" - send ALT, P, ENTER (to run). This code will not wait for the enter - it just loops and loops and loops. The sent keystrokes do work, but the pause is only for a millisecond. I have tried both ENTER and {ENTER} too....
Code:
      
 loop
 {
      Sleep, (secs*1000)  ;<------- this works every time
   ; ControlFocus, Windows Media Player
   Send {Alt}
   Send P
   Send {ENTER} ;  <----------- pauses for a millisecond or so
   SoundBeep, ,1500]
      KeyWait, Enter  ; <-------------- this doesn't do anything
      Send {Alt}
      Send P
      Send {ENTER}  ; <------------- runs after the pause
 }


And of course there is no way to end it ... :o

Thanks again to all

Mike :?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 4:47 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
Hi Mike,

You will have to post your "Esc::ExitApp" outside the auto-execute section (i.e. after the first Return encountered at the top of your script), otherwise it will get executed when the script starts. Try putting them at the end of the script instead (after any returns).

Try the below, see if this helps:

Code:
#NoEnv
SetTitleMatchMode, 2 ; match windows title more easily

secs := 5 ; will run every 5 seconds
SetTimer, PauseTimer, % secs*1000
Return ; this is the end of the auto-execute section

PauseTimer:
   SetTimer, PauseTimer, off ; so it won't get interrupted by the timer while it is running
   ControlSend,, ^p, Windows Media Player ; pause
   SoundBeep, ,1500
   KeyWait, Enter, D ; wait for enter to be pressed Down
   ControlSend,, ^p, Windows Media Player ; unpause
   SetTimer, PauseTimer, on ; restart the timer
Return

Esc::ExitApp ; exit when escape is pressed


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 10:24 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
I took your full-script posting and modified it to work.
I carefully went through your code (this time) and found why it wouldn't pause the player reliably and why it wouldn't wait for Enter from user.

I still don't believe that " Send {ENTER}" is needed, but I left it.

I tested it with a version of Media Player Classic, and then corrected a few commands to work with WMP instead. You will find MPC specific lines commented out.
In case you didn't know the symbols "/*" and "*/" are a pair that makes writing extended comments (or disabling code) easier.

Since you wrote that you wanted it to close WMP when B.D.T. was closed, I added the OnExit command, which makes things a little simpler.

The Keywait command was coded wrong. The Options parameter was not included, which defaulted to 'wait for key released' which it was if the key wasn't being held. Sortof reverse catch-22. :wink:

Code:
settitlematchmode 2 ; <--- allows easier title match

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

OnExit, quit ;<------------ funnel all requests to exit through this 'quit' subroutine

#SingleInstance force

; HotKey, Esc, on ; Assign a hotkey to terminate this script.


MsgBox, 4,Bretts DVD timer , This will start and run Bretts DVD timer program`n `nWould you like to continue?, 605  ; 60-second timeout.
   IfMsgBox, No
      ExitApp ; User pressed the "No" button.
     
   InputBox, secs, Set Timer, Enter the number of seconds for the program to run between prompts?, , , 250 , 50 , , , 60 , 60
      time=%secs%*1000
      IfMsgBox Timeout, ExitApp

   
; check to see if player active
   IfWinActive, Windows Media Player
      WinMaximize  ; Maximizes the window found by IfWinActive above.
         Else run "C:\Program Files\Windows Media Player\wmplayer.exe" /open
      Sleep 1000

/*======================================media player=========================
; check to see if player active
   IfWinActive, Media Player
      WinMaximize  ; Maximizes the window found by IfWinActive above.
         Else run "C:\Program Files\MPC HomeCinema\mpc-hc.exe"
      Sleep 1000
======================================media player=========================
*/

;   IfWinActive, Media Player

   IfWinActive, Windows Media Player
      MsgBox, 4145, Do This Now!!, While this window is up insert the DVD into the player and start it playing.`n  `nThe DVD will play for %secs% seconds then pause for input.`n Once the DVD is playing select OK to start the timer. `nHitting "Cancel" will EXIT the program., 120
      IfMsgBox, Cancel
   ExitApp


******************************************************
******************************************************
; HERE IS WHERE MY NEXT CHALLENGE IS - THIS WILL WAIT THE RIGHT NUMBER OF SECONDS,
; AND SEND A SERIES OF COMMANDS THAT WIL PAUSE MEDIA PLAYER - THAT WORKS...
; KEYWAIT DOES NOT WORK... IT JUST SKIPS RIGHT BY IT
; I WANT THE PROGRAM TO PAUSE WHERE THE KEYWAIT IS ... AND WAIT FOR {ENTER}
; THEN RUN THE LOOP AGAIN....
;
; i ALSO NEED TO ASSIGN A HOTKEY SO {escape} WILL QUIT THE APP WHENEVER IT IS PRESSED.
; I TRIED A LINE FOR THAT UP TOP BUT IT DIDN'T WORK....
     
loop
{
   Sleep, (secs*1000)

;   ControlFocus, Windows Media Player

;   Send {Alt} ;<---- this doesn't work well to send the characters separately
;   Send P

   send ^p   ; normal way to send control-p // this should PAUSE the WMP
;   send {space}   ; media player classic

      ; some systems may be stubborn and require an alternate control-p               
      ; for now this next line is commented out and will not be run
   ; send {control down} p {control down} 

   Send {ENTER}
   SoundBeep, ,1500]
;      KeyWait, {Enter}
      KeyWait, Enter, D   ; <--- wait for enter from user, this is the correct format of the command

   send ^p   ; normal way to send control-p // this should UNPAUSE the WMP
;   send {space}   ; normal way to send control-p // this should UNPAUSE the WMP

}

;=================== escape key closes the program ===========================
esc::
  exitapp

;=================== if the program closes, so does WMP ======================
quit:
WinClose, Windows Media Player
;winclose, Media Player ; this is for Media Player Classic

exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject: THANK YOU - THANK YOU
PostPosted: March 18th, 2010, 2:13 am 
Offline

Joined: March 13th, 2010, 10:10 pm
Posts: 4
I can not possibly thank each of you enough. I have finally come up with a version that works, pulling pieces and tips from everyone that helped.

I think I have it exactly as I need it. Compiled it to an exe file in a directory with the images I need Now I will be loading it on the schools computer, and probably making a few adjustments. Mission completed. I will be back if I need more help in the future.

Thank you all so much

Mike
[/img]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2010, 4:08 am 
Offline

Joined: April 22nd, 2008, 7:13 pm
Posts: 82
Hi Mike!
I´m a bit bummed that I missed assisting you on this script. I´m a special needs teacher and have made a similar script to work with VLC player. I salute you for giving it a go and doing it yourself, most parents I know can´t muster the energy to do something like this. If you ever need any help with any scripts, or have any ideas for new ones please let me know and I´d love to assist you to make something.
Something that could be added to your script pretty easily is to pause the movie, display a letter to the screen and your child must press the same letter on the keyboard to restart the movie. That would give a bit more of a challenge to your son if you think it is within his ability.

Here´s two of my apps which I´m quite proud of, I have a few others which I have not posted as yet if you´re interested. They are both free and open source so if you want to give them to your son´s school you´re more than welcome (in fact I would appreciate it).

MouseTrainer (my personal favourite)
http://www.autohotkey.com/forum/viewtop ... usetrainer

Typing Tutor
http://www.autohotkey.net/~evandevon/Ty ... %202.2.zip
There are animations that play as a reward in this app but I can´t remember if I uploaded them all. I´ll check when I get home and have access to my files.

Also, there are updates to both the programs which I have to upload and can´t at the moment but they still work (with only occassional bugs;)

_________________
Inventing problems that need solutions...

Open Communicator
MouseTrainer


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Google Feedfetcher, mrhobbeys, rbrtryn and 56 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group