Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

How to do eternal loop with hotkeys


  • Please log in to reply
11 replies to this topic

#1 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 06:43 PM

G'day everyone

I'm sure my problem is a simple one to solve, but try as I might, I can't figure it out myself. I want to run a script for ever (until I kill it) which enables two hotkeys to be pressed at any time, at which time a certain set of script instructions are followed.

It is for a "text editor" (roughly speaking, to keep it simple) and a spell-checker. The spell-checker takes quite a bit of time to load at first, so it's better to keep the spell-checker running continuously and simply activate the spell-checker once the text to be spell-checked has been copied to the clipboard.

Here is my code:

; Script Function:
; Does a spell-check on some text from OmegaT (the text editor)
; in Kastrul (the spell-checker). The script should run in the
; background forever, and two functions should be activated by
; two hotkeys. The user will always use Ctrl+Enter, but may
; sometimes not use Alt+Enter first before using Ctrl+Enter again.

; Let the script stay resident in memory and let the two hotkeys
; be active... I can't figure out if the HotKeySet must come before
; or after or inbetween the while-loop.

While 1
Sleep (100)
WEnd

HotKeySet ("+{ENTER}", "DoSpellCheck")
HotKeySet ("!{ENTER}", "UnSpellCheck")


; When Ctrl+Enter is pressed, wait for OmegaT window to be active
; (it should be already), do Ctrl+A (highlight all), do Ctrl+C (copy),
; activate Kastrul (the spell-checker), wait for it to be active,
; wait 100 cycles, do Ctrl+V (paste, which in Kastrul activates the
; spell-checker).

Func DoSpellCheck()
WinWaitActive("OmegaT", "")
Send("+a")
Send("+c")
WinActivate ("Kastrul", "")
WinWaitActive("Kastrul", "")
Sleep(100)
Send("+v")
EndFunc

; When Alt+Enter is pressed, activate the OmegaT window, and wait
; for it to become active.

Func UnSpellCheck()
WinActivate ("OmegaT", "")
WinWaitActive("OmegaT", "")
EndFunc


Any help is appreciated. The help files tell me how to set hotkeys and how to keep a script running indefinitely, but not how to do both, and the examples given seem to be for scripts that exit once the hotkey has been pressed.

One alternative might be to forget about the endless loop, and simply let the script restart itself when it ends... what do you think? Not very elegant, though.







#2 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 05 March 2006 - 07:35 PM

HotKeySet("+{ENTER}","DoSpellCheck") HotKeySet("!{ENTER}","UnSpellCheck") While 1 sleep(1000000) WEnd Func UnSpellCheck() WinActivate ("OmegaT", "") WinWaitActive("OmegaT", "") EndFunc Func DoSpellCheck() WinWaitActive("OmegaT", "") Send("+a") Send("+c") WinActivate ("Kastrul", "") WinWaitActive("Kastrul", "") Sleep(100) Send("+v") EndFunc

that ought to do it :o
~cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

#3 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:20 PM

I'm sure my problem is a simple one to solve, but try as I might, I can't figure it out myself....


Well, this is embarrasing. I tried to use + for Ctrl instead of ^. Duh...

As for the eternal loop, it seems that the following does the trick nicely:

While 1
HotKeySet ("^{ENTER}", "DoSpellCheck")
HotKeySet ("!{ENTER}", "UnSpellCheck")
WEnd


My next project will be to use the same hotkey for both functions (an if statement ought to do it... if I can figure out where to put it).

#4 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 05 March 2006 - 08:28 PM

i wouldnt put the hotkeys IN the While statement, put them b4 and make sure you havea sleep (of at least 10) in your while loop, otherwise it really chews up memory/CPU Usage
~cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

#5 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:29 PM

My next project will be to use the same hotkey for both functions (an if statement ought to do it... if I can figure out where to put it).


Easy... did it with Case statements:

While 1

$var = 0

If WinActive ("OmegaT", "") Then $var = 1
If WinActive ("Kastrul", "") Then $var = 2

Select
Case $var = 1
HotKeySet ("^{ENTER}", "DoSpellCheck")
Case $var = 2
HotKeySet ("^{ENTER}", "UnSpellCheck")
EndSelect

WEnd



#6 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:30 PM

I wouldnt put the hotkeys IN the While statement ... it really chews up memory/CPU Usage.


Thanks... which is better on the CPU (and other factors) -- a high sleep number (10000) or a low sleep number (10)?

#7 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:36 PM

Hmmm, if i put the While-WEnd statement separately, the following Case statement method doesn't work:

$var = 0

If WinActive ("OmegaT", "") Then $var = 1
If WinActive ("Kastrul", "") Then $var = 2

Select
Case $var = 1
HotKeySet ("^{ENTER}", "DoSpellCheck")
Case $var = 2
HotKeySet ("^{ENTER}", "UnSpellCheck")
EndSelect

While 1
Sleep (100)
WEnd


Anyone got any other ideas for me?

#8 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 05 March 2006 - 08:37 PM

higher, and if you only have the WHILE...WEnd there for keeping the script going i'd do like
While 1 sleep(10000000) WEnd

Edited by cdkid, 05 March 2006 - 08:37 PM.

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

#9 greenmachine

greenmachine

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,252 posts

Posted 05 March 2006 - 08:39 PM

If you make the loop outside of the Select statement, maybe you should use if WinExists rather than WinActive. With the current setup, you most likely don't have the window active in time for when the script runs through that line, and it doesn't change the $var from 0.

#10 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:49 PM

Anyone got any other ideas for me?


Okay, I solved it by using only one function, and placing the If...ElseIf statement inside the function:

HotKeySet ("^{ENTER}", "SpellCheck")

While 1
Sleep (100)
WEnd

Func SpellCheck()

If WinActive ("OmegaT", "") Then

WinWaitActive ("OmegaT", "")
Send ("^a")
Send ("^c")
WinActivate ("Kastrul", "")
WinWaitActive ("Kastrul", "")
Sleep (100)
Send ("^v")

ElseIf WinActive ("Kastrul", "") Then

WinActivate("OmegaT", "")
WinWaitActive("OmegaT", "")

EndIf

EndFunc



#11 leuce

leuce

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 277 posts

Posted 05 March 2006 - 08:52 PM

If you make the loop outside of the Select statement, maybe you should use if WinExists rather than WinActive.


Hmm, I'm not sure if that'll work, since both windows do exist all the time that the script runs, but only one of them is active.

#12 greenmachine

greenmachine

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,252 posts

Posted 05 March 2006 - 09:32 PM

Ok, I didn't know both existed all the time. I was just saying that because normally when I run a script, Scite is the active window if not compiled, and windows explorer (for the folder) is active when it is compiled. So I thought that your script wasn't getting the right active window because it checked too fast for you to react.

Well it looks like you figured out what you wanted to do, so I don't need to make any more suggestions.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users