Question about complex loops - autohotkey

I'm writing a script in AutoHotKey, and I've stumbled upon an impasse. Right now I have a working GUI that involves checkboxes and dropdown lists. The program is a macro script for gaming-- the user checks which macros he wants running in the background, submits his options, and the program constantly evaluates various conditions and executes commands based on whether or not those conditions are satisfied.
The template of each one of the macros is this:
Loop:
PixelGetColor, color, 488, 778 ;gets pixel color of specific coordinate
if thecolor = 0x000000 ; if the color is black
{
SendInput {Raw}f ;sends the f command to use item
}
else sleep 20
goto, Loop
So the macro loops, evaluating a pixel each 2 miliseconds, and sending input if the if statement is fulfilled.
Here is the problem:
For each one of the macros that the user can check to run in the background, there is a different pixel color pinpointed, a different input sent, and a different coordinate to evaluate. How can I make an all-encompassing loop that takes into account the users check-box choices?
For example, if the checkBox = 1 (selected), I could be like:
if(%Box1%=1)
{
Run above code
}
But if the user checks 1 and 2, I'll have to evaluate other coordinates, too. Like thus:
if(%Box1%=1 & %Box2%=2)
{
Run above code
+
PixelGetColor, color2, 510, 778
if thecolor = 0x000000
{
Send 1
Sleep 20000
goto, start
}
else, sleep 20
I don't want to have multiple different loops that run one after another because it would too slow, and I don't want to have to do a huge branch of all the possible 'if' permutations.
Does anybody know what to do?

and I don't want to have to do a huge
branch of all the possible 'if'
permutations
The problem my friend is that that is the only solution.
You have to check the status of each checkbox, so there is no jumping that part.
If I could see your code I could send you an amendment for it, but you cannot paste the full code here. Try autohotkey.net/paste, and I will hopefully be able to help you.

Related

autohotkey: unable to send numbers as integers (sends like a string)

6::
send 6
return
also tried
$6::send 6 return
$3::send 3 return
$5::send 3 return
Nor did blind or text or replacing send with any of the other sendmode names on the send doc page achieve the effect, which is, for a game to recognize these sends as numbers. These work while typing in chatbox ingame, i.e. "hello14674355", but not when selecting from a menu that use integers, i.e. "press a number from 1-9 to activate an option in a menu". I need ideas/help on what else i can try or do to get this to work. I have a situation where some numbered keys are hotkeys on certain condition (when im not typing for ex), this requires I manually setup the condition where they do send their own value. I need a fix for that. Is there way to retain their normal function as default, but check it only after the hotkey behaviour conditions? When they r in hitkey mode i need them to not send their own value but the hotkey.
Can I get ~3:: if(){... to work infront of 3's native function? then i can return early maybe so it doesnt proc the native 3 sending?

How can I set up a macro to send a command in a discord channel at specified times?

I'm trying to set it up so that I can activate a script on AutoHotKey that will use my discord app in the background and send a specified message every three hours. It would be best if the three hours were set on specified times, such as 12, 3, 6, 9, 12, etc.
I'm mostly having a hard time understanding the send command, but I'm sure I need information on how to get it to use the discord api properly as well, so the more you give me the better. I was going to do this using the chrome web browser and opening a tab with the discord website on there so it didn't interfere with anything I'm doing, but I figure now that doing it through the application will be the best way.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
Send, #
Send, c
Send, h
Send, Enter
All I got when trying this code is the windows ding sound. I have not learned enough of this stuff yet, so again, the more direction I can get here the better.
I would recommend reading through the "Getting started" section of the help documentation to get a solid understanding of the basics.
https://www.autohotkey.com/docs/AutoHotkey.htm
Looking at what you have so far, it's important to understand what you're sending. Each of these, !^+#, act as one-time modifier keys alt, ctrl, shift, and windows-key. If you want to send these as actual characters, you need to enclose these in braces - like this, Send , {#}. The same goes for the enter-key; without the braces, it will just send the letters E, n, t, e, and r. Additionally, you can combine all of your sends into one - like so, Send , {#}ch{enter}.
As for setting it to run on every third hour, you can accomplish this with SetTimer. I would recommend checking it out in the help documentation for usage and examples. If you want it to run once every three hours, that is simple enough; if you want it to run on hours divisible by three, then you'll have to also have it check the time. Here is an example that checks every hour if the hours of the current time are divisible by three.
f1::
GoSub, l_Timer
SetTimer , l_Timer , 3600000 ; 60min/hour x 60sec/min x 1000ms/sec
Return
l_Timer:
If ( Mod( SubStr( A_Now , 9 , 2 ) , 3 ) = 0 )
MsgBox , The current hour is divisible by 3.
Return
Note that on the first run, the timer must run out before activating the sub-routine. You can make it run immediately first by adding a GoSub , [label] line as I did.

Seeing which part of code MatLab is currently running [duplicate]

Is there any way to stop the execution of a matlab program from the debugger like ctrl+c does, but then being able to continue execution (like you can in say c#)?
If not, is there any better way to workaround this other than trying to pre-emptively set break points or dbstop statements in your matlab code?
I would like to be able to interrupt a long running simulation to look at the current state and then continue the simulation.
The two options I'm currently using/considering are
dbstop commands (or (conditional) breakpoints) in the code.
Drawback is that sometimes I don't want to stop the simulation for a few hours, sometimes want to stop after only a few seconds (and I don't necessarily know that in advance) and this doesn't work well with this approach: If I set the break condition to break every 5 minutes, I can't leave matlab running for hours without interaction. If I set the condition to higher, I have to wait too long for the condition to hit.
include code to save the workspace every few seconds/minutes and import the workspace into a second matlab instance. Drawback is that this is a huge hassle and also doesn't necessarily allows me to resume the simulation with the state of the saved workspace then step through the code for a few iterations.
I'm hoping there is a better solution than either of the 2. Thanks for any advice!
Edit: I think what I'm going to do is write simple matlab function that checks an environment variable or a file on disk every iteration and calls dbstop if I set a flag in this file or env. This way I can control when (and if needed which of several) the breakpoint hits from outside matlab by editing the file. Messy, but should work.
This is not necessarily the best way, but you could simulate a file-based signal/interrupt framework. It could be done by checking every once in a while inside the long simulation loop for the existence of a specific file. If it does, you enter interactive mode using the keyboard command.
Something along the lines:
CHECK_EVERY = 10; %# like a polling rate
tic
i = 1; %# loop counter
while true %# long running loop
if rem(i,CHECK_EVERY) == 0 && exist('debug.txt','file')
fprintf('%f seconds since last time.\n', toc)
keyboard
tic
end
%# ... long calculations ...
i = i + 1;
end
You would run your simulation as usual. When you would like to step in the code, simply create a file debug.txt (manually that is), and the execution will halt and you get the prompt:
2.803095 seconds since last time.
K>>
You could then inspect your variables as usual... To continue, simply run return (dont forget to temporarily rename or remove the file). In order to exit, use dbquit
EDIT: Just occurred to me, instead of checking for files, an easier solution would be to use a dummy figure as the flag (as long as the figure is open, keep running).
hFig = figure; drawnow
while true
if ~ishandle(hFig)
keyboard
hFig = figure; drawnow
end
%# ...
pause(0.5)
end
With the release of R2016a, you can just hit the Pause button in the code editor and it will halt right away. The keyboard shortcut is Ctrl+F5.
To pause the execution of a program while it is running, in the Editor tab, click the Pause button. MATLAB pauses execution at the next executable line*.
When your code is running, the Start button will turn into a pause:
Another change with this release is the ability to add/remove breakpoints while running. Previously you couldn't do this, apparently.
You can set a conditional breakpoint in the MATLAB Editor. You can also use DBSTOP to do this. For example, this will set a conditional breakpoint in the file myFcn at line 20 which will stop execution when a loop variable i is a multiple of 500:
dbstop in myFcn.m at 20 if rem(i,500) == 0
Then you can continue execution after you inspect some of your variables.
If saving the workspace to a file is a good proxy for what you want, how about making a simple GUI with a toggle button. In your code, check the state of the button. If the button is depressed, save the state, update a static text to reflect time stamp of last save, unpress the button. Optionally, have a conditional breakpoint based on the state of that toggle button.
Here is an alternate solution using the waitinput File Exchange submission.
The advantage is that you can use it from whithin the current session or in cases where it is troublesome to set up a file. Also it won't leave a file behind on the computer.
The downside is there as well unfortunately, you need to wait for the checking moment before you can terminate and it costs a little bit of time.
for t = 1:10
pause(3) %Doing some calculations
str = waitinput('Enter 1 if you want to stop ',5);
if ~isnan(str)
keyboard; % Enter dbcont if you want to continue from here
end
['moving on, it is now: ' datestr(now)]
pause(3) %Doing some more calculations
end
If you want, you can prevent lines being printed to the screen. In this case you need to enter the input at the time the figure window is open (Look in your start bar on windows).
To summarize, the short code that you can put somewhere like a conditional breakpoint would be:
if ~isnan(waitinput('',5))
keyboard;
end
After certain version (I don't know which one exactly):
Windows: Ctrl + F5
Mac: Command + F5 (I guess)
Unix: I am looking for answer too
After 2016a, there is a button for that on the interface too.

{AHK} Defining controls (/Assigning tasks) for those GUI buttons that were creted after script launch

Lets say with some action during running script, you create gui with several buttons via Loop.
Loop, 5
{
Gui, Add, Button,, Number %A_Index%)
}
Gui, Show
How do u then assign actions upon pressing one of buttons?
It seems you cant do it after script launch, and tricks like
ButtonNumber%A_Index%: ;even if i was doing it inside loop.
do something here bla bla
return
do not work.
To do things even worse, i wanted to created these buttons (here for test) from contents of a file, say, each lines text gets utilized to name a button.
you can find similar mini-projects in AHK help files. Buts lets stick with this simple analog.
May be:
Storing and Responding to User Input, third option Variable or g-label is the anwser. Yet it asks for static/global var, but i have troubles declaring these. And g-labels i am not familiar with.
Other option i had in mind is- creating pre-defined buttons (a lot), rename them to my values (from file), and discard rest. hopefully i will be able to use predefined controls.
P.S
AHK help file is a real mess, as a beginner i find it pretty had to fish out complete and meaningful information, instead you have to search and take a bite here and there.
One way is to use a parsing loop and one g-label for all the buttons, then use A_guiControl to get the variable name of the button that called the sub-routine
Example:
; fileread, file_content, Path-to-file
file_content =
(
line with text one
line with text more
line with text other
line with text something
line with text two
)
Loop, parse, File_content, `n, `r
{
Gui, Add, Button, vMyButton%A_index% gButtons, %A_LoopField%
}
Gui, Show
return
Buttons:
msgbox % A_GuiControl
return
GuiClose:
ExitApp
Hope it helps
I have found one possible anwser to my problem. Basicly it involves g-label functionality that blackholyman (lel) suggested.
Using same g-label for all my buttons combined with A_GuiControl comparison inside button control.
Since i have stored buton names in a file, in one line with other data, that is relevant for this button, i can compare each line, by parsing, with button name (A_GuiControl), thats makes me able to retrieve relevant data inside assigned g-label.
May be some one will find it useful. Ill add code later.

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf