I'd like to run a program when any key is pressed with AutoHotKey
Something like:
AnyKey::Run, D:\my\program\to\run\on\any\key.bat
EDIT2:
This code is working perfectly:
#InstallKeybdHook
SetTimer, AnyKeyPressed, 100
AnyKeyPressed:
if( A_TimeIdlePhysical < 100 ){
Run, D:\my\program\to\run\on\any\key.bat
}
^!p::pause
A simple solution:
#InstallKeybdHook ; this MUST be called at the start of your script
AnyKeyPressed() ; returns a 1 if any keyboard key is pressed, else returns 0
{
if( A_TimeIdlePhysical < 25 )
return 1
return 0
}
Note this function will return 1 if any key is pressed OR being held down, so change your code appropriately.
What happens is; the #InstallKeybdHook will change the behaviour of A_TimeIdlePhysical to only look for keyboard events.
You have to check A_TimeIdlePhysical periodically, not just once on script start:
#InstallKeybdHook
SetTimer, CheckActivity, 100
Exit
CheckActivity:
if(A_TimeIdlePhysical < 100) {
Run, myNastyPictureMaker.bat
ExitApp
}
return
You can use SetTimer for recurring tasks. The script stops when the first activity was detected; otherwise, it would take a picture every 100 ms (or whatever timeout you set).
P.S: I hope you only want to use such a script on your private PC and not some publically available computer...
Use Input, AnyKey, L1 to wait for any key to be pressed. L1 means after one key was pressed, without a [end] key required. You can check the content of AnyKey, but don't really need to.
Perhaps a list of known keys might work?
keys = ``1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./
Loop Parse, keys
Run, D:\my\program\to\run\on\any\key.bat
return
This is what comes to mind.
#Persistent
#InstallKeyBDHook
SetTimer, WaitingForKey, 100
Return
WaitingForKey:
Input, LogChar, B I L1 V
LogWord := LogWord . LogChar
ToolTip, % LogWord
;Run, D:\my\program\to\run\on\any\key.bat
LogWord:=
Return
^!p::pause
Related
I'm running an Autohotkey script that auto capitalizes the first character of a sentence (for example in Texstudio or Chrome). The script (specifically the loop I guess) sometime causes 30–40% of CPU. Therefore, I am wondering if there is a possibility to optimize the code (maybe without using loop?) to reduce the CPU usage. Thanks in advance. Here is the code:
#SingleInstance force
#NoEnv
SetBatchLines -1
Loop {
if WinActive("ahk_exe texstudio.exe") or WinActive("ahk_exe chrome.exe")
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Tab}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key) {
If state = 1
state = 2
} Else {
If state = 2
Send {BS}{%key%}
state =
}
}
Return
SetTimer consumes much less CPU because of the period.
#SingleInstance force
#NoEnv
#Persistent
; SetBatchLines -1
; create a group of the programs in which you want auto-capitalize
GroupAdd, auto_capitalize_group, ahk_exe texstudio.exe
GroupAdd, auto_capitalize_group, ahk_exe chrome.exe
SetTimer, auto_capitalize, 300 ; check every 300 ms
Return
auto_capitalize:
if !WinActive("ahk_group auto_capitalize_group")
return ; do nothing
; otherwise:
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Tab}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key)
{
If state = 1
state = 2
}
Else
{
If state = 2
Send {BS}{%key%}
state =
}
Return
I am trying to navigate my mouse on object but I want to create a condition that will check if "surowiec" is still on the screen, if not I want to skip loop and go to another one. After it finish the second one get back to first and repeat.
[error] script [ Documents ] stopped with error in line 12 [error] FindFailed ( can not find surowiec.png in R[0,0 1920x1080]#S(0) )
w_lewo = Location(345,400)
w_prawo = Location(1570,400)
w_gore = Location(345,400)
w_dol = Location(345,400)
surowiec = "surowiec.png"
while surowiec:
if surowiec == surowiec:
exists("surowiec.png")
if exists != None:
click("surowiec.png")
wait(3)
exists("surowiec.png")
elif exists == None:
surowiec = None
click(w_prawo)
wait(8)
surowiec = surowiec
How about a small example:
while True:
if exists(surowiec):
print('A')
click(surowiec)
else:
print('B')
break
A while loop that is True will always run, until it it meets a break to exit the loop. Also have a look at the functions that are available in Sikuli, it can somethimes be hard to find them, that they are available. So here are a few nice ones:
Link: Link 1 and Pushing keys and Regions
The commands that I found myself very usefull are is exists and if not exists, and find that will allow to locate an image on the screen. Then you don't have to find an image over and over again if it stays on the same location. image1 = find(surowiec)
Goal of the script: to continually press Numpad0 for 10 seconds each time hotkey is pressed.
Current code:
toggle = 0
#MaxThreadsPerHotkey 2
timerToggle:
Toggle := !Toggle
sleep 10000
Toggle := !Toggle
F12::
SetTimer, timerToggle, -1
While Toggle{
send {NumPad0}
sleep 100
}
return
At present, the script will run as intended, but only once. Attempting to run it again after the first time does nothing. What am I missing?
I would rather use SetTimers instead of a 10 sec. long while like so
F12::
Send {Numpad0}
SetTimer, start, 100
SetTimer, stop, -10000
return
start:
Send {Numpad0}
return
stop:
SetTimer, start, off
return
Your script likely doesn't toggle your variable correctly. Here is a cleaner version of what you are trying to do which uses A_TickCount:
F12::SetTimer, HoldNumPad, -1
HoldNumPad:
kDown := A_TickCount
While ((A_TickCount - kDown) < 10000)
{
Send {Numpad0}
Sleep 100
}
Return
Note that pressing F12 while the label is running will not have any affect.
EDIT: Made SetTimer use -1 period to run only once, thanks to MCL.
ArrayCount = 0
Loop, Read, Times.txt ; This loop retrieves each line from the file.
{
ArrayCount += 1 ; Keep track of how many items are in the array.
ArrayTime%ArrayCount% := A_LoopReadLine
}
WinGetTitle, Title, A
Loop %ArrayCount%
{
element := ArrayTime%A_Index%
Time = %A_WDay%%A_Hour%%A_Min%
msgbox %Time% , %element%
if (Time=%element%)
{
IfWinExist, Test.txt
{
WinActivate
Sleep 500
Send Hi{enter}
msgbox %Time% , %element%
Sleep 500
WinActivate, %Title%
}
}
}
Ok so the main issue is with this part:
if (Time=%element%)
I have also tried
if (%Time%=%element%)
if (A_WDay . A_Hour . A_Min=%element%)
And I think some other similar variations, the problem I'm getting is it's either always true, or always false, depending on how I have it written.
Inside the text file is a list like this:
10000
10700
11400
20400
21100
I add an extra line that has the current time for testing, and I added the msgbox to compare, and I can clearly see they're both the same when it doesn't work, or that they're different when it does. Sorry for such a basic question but I feel like I've really been trying for a long time and read everything I can on variables and IF statements, thanks for any help.
Also the point of it is I need it to go off every 7 hours starting at midnight on sunday, this is what I came up with, if there's maybe a completely better way in general I'd be happy to hear that too.
Try this:
if % Time = element
{
MsgBox, Equal!
}
As for the scheduling part, try running your script through Windows Task Scheduler (hit Windows+R, type taskschd.msc and press Enter). There are tutorials on the Internet explaining how to create new tasks.
With regard to timers, have a look at this as an example.
SetTimer, AlertType1, 60000
ToAlertType1:=1
ToAlertType2:=1
AlertType1:
;If A_WDay between 2 and 7 ; is day monday - sunday?
;{
If (A_Hour = 7 or A_Hour = 13)
{
If (ToAlertType1)
{
SoundBeep, 500, 500
ToAlertType2:=1
ToAlertType1:=0
MsgBox, 4096,%AppName%, Msg1.
Return
}
}
Else if (A_Hour = 21)
{
If (ToAlertType2)
{
SoundBeep, 500, 500
ToAlertType2:=0
ToAlertType1:=1
MsgBox, 4096,%AppName%, Msg2.
Return
}
}
;}
Return
I would like to create a timer using Lua, in a way that I could specify a callback function to be triggered after X seconds have passed.
What would be the best way to achieve this? ( I need to download some data from a webserver that will be parsed once or twice an hour )
Cheers.
If milisecond accuracy is not needed, you could just go for a coroutine solution, which you resume periodically, like at the end of your main loop, Like this:
require 'socket' -- for having a sleep function ( could also use os.execute(sleep 10))
timer = function (time)
local init = os.time()
local diff=os.difftime(os.time(),init)
while diff<time do
coroutine.yield(diff)
diff=os.difftime(os.time(),init)
end
print( 'Timer timed out at '..time..' seconds!')
end
co=coroutine.create(timer)
coroutine.resume(co,30) -- timer starts here!
while coroutine.status(co)~="dead" do
print("time passed",select(2,coroutine.resume(co)))
print('',coroutine.status(co))
socket.sleep(5)
end
This uses the sleep function in LuaSocket, you could use any other of the alternatives suggested on the Lua-users Wiki
Try lalarm, here:
http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
Example (based on src/test.lua):
-- alarm([secs,[func]])
alarm(1, function() print(2) end); print(1)
Output:
1
2
If it's acceptable for you, you can try LuaNode. The following code sets a timer:
setInterval(function()
console.log("I run once a minute")
end, 60000)
process:loop()
use Script.SetTimer(interval, callbackFunction)
After reading this thread and others I decided to go with Luv lib. Here is my solution:
uv = require('luv') --luarocks install luv
function set_timeout(timeout, callback)
local timer = uv.new_timer()
local function ontimeout()
uv.timer_stop(timer)
uv.close(timer)
callback()
end
uv.timer_start(timer, timeout, 0, ontimeout)
return timer
end
set_timeout(1000, function() print('ok') end) -- time in ms
uv.run() --it will hold at this point until every timer have finished
On my Debian I've install lua-lgi packet to get access to the GObject based libraries.
The following code show you an usage demonstrating that you can use few asynchronuous callbacks:
local lgi = require 'lgi'
local GLib = lgi.GLib
-- Get the main loop object that handles all the events
local main_loop = GLib.MainLoop()
cnt = 0
function tictac()
cnt = cnt + 1
print("tic")
-- This callback will be called until the condition is true
return cnt < 10
end
-- Call tictac function every 2 senconds
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 2, tictac)
-- You can also use an anonymous function like that
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1,
function()
print( "There have been ", cnt, "tic")
-- This callback will never stop
return true
end)
-- Once everything is setup, you can start the main loop
main_loop:run()
-- Next instructions will be still interpreted
print("Main loop is running")
You can find more documentation about LGI here