Sign in to follow this  
Followers 0

How to do eternal loop with hotkeys

12 posts in this topic

Posted

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.

Share this post


Link to post
Share on other sites



Posted

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

Share this post


Link to post
Share on other sites

Posted

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).

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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)?

Share this post


Link to post
Share on other sites

Posted

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?

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0