I am really new to programming, I tried looking for answers, but can't find any. Can somebody help me?
I want to create a script where you press a certain mouse button, the button will perform different keys with different timing. For example: I click the right mouse click, the following will be executed:
W-SPACE-SHIFT-W-W
I know how to do this, but how do you turn it into a script where the interval is different each time? Like I press the mouse button then this happens: W - (delay between 0.5 sec and 1 sec) - SPACE (delay between 0.5 sec and 0.7 sec) - SHIFT (delay between 0.3 and 0.35) etc.
I thought this was the start:
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
--keyboard keys will be executed all with a random interval between the keys
end
end
It's a really simple script, but I don't know how to make it work. Help would be really appreciated.
Good day,
-Joël
You can do this using AutoHotKey, listening for the RButton event and using the Sleep function to delay the kepresses. For example;
(Note: you could probably use variables with random delays using math functions)
RButton::
Send, w
Sleep, 500
Send, {Space}
Sleep, 500
Send, {Shift}
; etc...
Return
Of course, you can do more advanced scripts than this, AHK is a really simple but advanced piece of software that you can use to do nearly anything (Including calling obscure DLLs). People have designed libraries to do anything you want with it as well.
This might not be Lua, but it might be what you are looking for! ;)
Related
First question is: Is this script as fast as possible as it can get?
I'm talking about the commands in the start are they necessary/unnecessary? do they help at all when its about simple key remapping?
I would like it to run as fast as possible since I do pretty intense & fast stuff with this script.
The 2nd question is: How to prevent this script from spamclicking?
If I keep "E or R" held down, it will spam click which I do not want it to do.
How to fix this?
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
*e::Click
return
*r::Click
return
Ins::Suspend
return
Answer to the second question, add the KeyWait command to your hotkeys, KeyWait - Syntax & Usage | AutoHotkey
*e::
Click
KeyWait, e
return
*r::
Click
KeyWait, r
return
Answer to the first question, it looks like you may have gotten the header lines from here: How to optimize the speed of a script as much as possible. The post explains each line (see below) and even includes some benchmarks for other things.
In your case, if you're only doing simple key remapping, it's probably not worth including any of these lines with exception to SendMode , Input, as that is the most reliable send mode. You mention doing "pretty intense & fast stuff with this script"; what this means exactly will determine if the header lines are necessary.
Notes
1. #NoEnv is recommended for all scripts, it disables environment variables.
2. The default #MaxHotkeysPerInterval along with #HotkeyInterval will stop your script by showing message boxes if you have some kind of rapid autofire loop in it. Just put some insane unreachable high number to ignore this limit.
3. ListLines and #KeyHistory are functions used to "log your keys". Disable them as they're only useful for debugging purposes.
4. Setting an higher priority to a Windows program is supposed to improve its performance. Use AboveNormal/A. If you feel like it's making things worse, comment or remove this line.
5. The default SetBatchLines value makes your script sleep 10 milliseconds every line. Make it -1 to not sleep (but remember to include at least one Sleep in your loops, if any!)
6. Even though SendInput ignores SetKeyDelay, SetMouseDelay and SetDefaultMouseSpeed, having these delays at -1 improves SendEvent's speed just in case SendInput is not available and falls back to SendEvent.
7. SetWinDelay and SetControlDelay may affect performance depending on the script.
8. SendInput is the fastest send method. SendEvent (the default one) is 2nd place, SendPlay a far 3rd place (it's the most compatible one though). SendInput does not obey to SetKeyDelay, SetMouseDelay, SetDefaultMouseSpeed; there is no delay between keystrokes in that mode.
9. If you're not using any SetTimer, the high precision sleep function is useful when you need millisecond reliability in your scripts. It may be problematic when used with SetTimer in some situations because this sleep method pauses the entire script. To make use of it, here's an example that waits 16,67 milliseconds:
DllCall("Sleep",UInt,16.67)
10. When using PixelSearch to scan a single pixel of a single color variation, don't use the Fast parameter. According to my benchmarks, regular PixelSearch is faster than PixelSearch Fast in that case.
11. According to the documentation (this text is found in the setup file), the Unicode x64bit version of AHK is faster, use it when available.
So I have to scan and manually enter 1200 scanner number every week for inventory. The process goes in this order:
Open Website < Click text box < Enter numbers < hit submit < wait 20 seconds < hit submit again (it makes me confirm i entered right) < Wait another 20 seconds.
At this point, the page refreshes, and IO start over at the Click text box part.
I already converted the numbers to barcodes and scan them instead of typing, I also use 2 screens with 8 open windows to make it a bit faster.
But I was wondering if there was a batch file or another way to automate the process?
Even with the scanner, and 8 open windows, it takes 4 hours roughly to do them all.
I have been searching on google for a few days, and decided to ask here since most the pages i read go back to this site.
http://www.autohotkey.com/
Awesome macro software. Very flexible for a macro and easy language to learn.
Try searching for automated keyboard/mouse macro software. Never used it, just a tip.
This question appears to be similar on SuperUser: Redirecting input from file to command-line program
It appears to be effective for telnet sessions - not sure about http sessions. You might look into TCL and Expect as options though.
This question already has an answer here:
How to abort a running program in MATLAB?
(1 answer)
Closed 7 years ago.
I write a long running script in Matlab, e.g.
tic;
d = rand(5000);
[a,b,c] = svd(d);
toc;
It seems running forever. Becasue I press F5 in the editor window. So I cannot press C-Break to stop in the Matlab console.
I just want to know how to stop the script. I am current use Task Manager to kill Matlab, which is really silly.
Thanks.
Matlab help says this-
For M-files that run a long time, or that call built-ins or MEX-files that run a long time, Ctrl+C does not always effectively stop execution. Typically, this happens on Microsoft Windows platforms rather than UNIX[1] platforms. If you experience this problem, you can help MATLAB break execution by including a drawnow, pause, or getframe function in your M-file, for example, within a large loop. Note that Ctrl+C might be less responsive if you started MATLAB with the -nodesktop option.
So I don't think any option exist. This happens with many matlab functions that are complex. Either we have to wait or don't use them!.
If ctrl+c doesn't respond right away because your script is too long/complex, hold it.
The break command doesn't run when matlab is executing some of its deeper scripts, and either it won't log a ctrl sequence in the buffer, or it clears the buffer just before or just after it completes those pieces of code. In either case, when matlab returns to execute more of your script, it will recognize that you are holding ctrl+c and terminate.
For longer running programs, I usually try to find a good place to provide a status update and I always accompany that with some measure of time using tic and toc. Depending on what I am doing, I might use run time, segment time, some kind of average, etc...
For really long running programs, I found this to be exceptionally useful
http://www.mathworks.com/matlabcentral/fileexchange/16649-send-text-message-to-cell-phone/content/send_text_message.m
but it looks like they have some newer functions for this too.
MATLAB doesn't respond to Ctrl-C while executing a mex implemented function such as svd. Also when MATLAB is allocating big chunk of memory it doesn't respond. A good practice is to always run your functions for small amount of data, and when all test passes run it for actual scale. When time is an issue, you would want to analyze how much time each segment of code runs as well as their rough time complexity.
Consider having multiple matlab sessions. Keep the main session window (the pretty one with all the colours, file manager, command history, workspace, editor etc.) for running stuff that you know will terminate.
Stuff that you are experimenting with, say you are messing with ode suite and you get lots of warnings: matrix singular, because you altered some parameter and didn't predict what would happen, run in a separate session:
dos('matlab -automation -r &')
You can kill that without having to restart the whole of Matlab.
One solution I adopted--for use with java code, but the concept is the same with mexFunctions, just messier--is to return a FutureValue and then loop while FutureValue.finished() or whatever returns true. The actual code executes in another thread/process. Wrapping a try,catch around that and a FutureValue.cancel() in the catch block works for me.
In the case of mex functions, you will need to return somesort of pointer (as an int) that points to a struct/object that has all the data you need (native thread handler, bool for complete etc). In the case of a built in mexFunction, your mexFunction will most likely need to call that mexFunction in the separate thread. Mex functions are just DLLs/shared objects after all.
PseudoCode
FV = mexLongProcessInAnotherThread();
try
while ~mexIsDone(FV);
java.lang.Thread.sleep(100); %pause has a memory leak
drawnow; %allow stdout/err from mex to display in command window
end
catch
mexCancel(FV);
end
Since you mentioned Task Manager, I'll guess you're using Windows. Assuming you're running your script within the editor, if you aren't opposed to quitting the editor at the same time as quitting the running program, the keyboard shortcut to end a process is:
Alt + F4
(By which I mean press the 'Alt' and 'F4' keys on your keyboard simultaneously.)
Alternatively, as mentioned in other answers,
Ctrl + C
should also work, but will not quit the editor.
if you are running your matlab on linux, you can terminate the matlab by command in linux consule.
first you should find the PID number of matlab by this code:
top
then you can use this code to kill matlab:
kill
example:
kill 58056
To add on:
you can insert a time check within a loop with intensive or possible deadlock, ie.
:
section_toc_conditionalBreakOff;
:
where within this section
if (toc > timeRequiredToBreakOff) % time conditional break off
return;
% other options may be:
% 1. display intermediate values with pause;
% 2. exit; % in some cases, extreme : kill/ quit matlab
end
I am charged with an old BASIC program that needs to be altered to activate microphone recording on a specific keypress. I'm having trouble finding out how.
Anyone here able to shed any light?
Thanks for any help.
Edit: I'm pretty sure it was originally written for GW-BASIC.
Since it sounds like you don't have any of the audio code written already, my advice is that you don't try to record from GW-BASIC. There are no built-in functions for accessing the sound card (SOUND and BEEP don't count, as they work with the PC speaker), and sending SoundBlaster control codes is unreliable at best in Windows. Use a secondary, Windows-native program to record.
As for the BASIC code, you're going to have to poll the keyboard. Example:
100 PRINT "Press any key to continue"
110 A$ = INKEY$
120 IF A$ = "" THEN GOTO 110
130 IF A$ = CHR$(1) THEN GOSUB 1000
140 PRINT "Rest of code goes here..."
1000 ' Ctrl+A triggered the microphone
1010 PRINT "Starting microphone recording."
1020 SHELL "otherprg --startrecording"
1030 RETURN
Substitute your preferred key code. If you use INPUT, there's a way--the KEY statement?--to make a function key insert text of your choice. Use KEY to insert, say, CHR$(2)+CHR$(13) (^B plus Enter) when the function key is pressed, then in every INPUT call scan the results for CHR$(2) using INSTR, and branch to the microphone code as desired.
This still won't work if you're using INPUT to read numbers, though. Seriously, unless the microphone recording case is extremely constrained, you're setting yourself up for hideous code that only mostly works.
EDIT: And all this is skating around the biggest problem: GW-BASIC is single-tasking. When you're recording from the mic, you're not able to do real work elsewhere in the program, and vice versa.
Making a small app, and I want a function to execute 50% of the time. So if I were to dbl click the exe half the time the function would execute, and the other half it wouldn't. I can't seem to find anyway to easily do this, the one solution I tried seemed to determine the chance on compile rather than on run. Thanks in advance!
Generate a random decimal number between 0 and 1. If it is greater than 0.5 run, if it is less than or equal to 0.5 do not run.
Don't forget to seed the randomizer! Otherwise it will always give you the same value every time. You seed it using "Randomize Timer", e.g.:
Private Sub Main()
Randomize Timer
If Rnd > 0.5 Then
ExecuteFunction ()
End If
End Sub
For example:
Private Sub Main()
If Rnd > 0.5 Then
ExecuteFunction ()
End If
End Sub
If you want it to randomly run, others have already provided that solution. If you want a more deterministic behavior (it must run exactly every second time), you will need to store state between executions.
You can save the state in either the registry or on the file system by (for example) attempting to read an integer from a file (set it to zero if the file's not there), add 1 and write it back to the same file.
If the number written back was even, run your function otherwise exit.
That way, you'll alternate between execute and don't-execute.