AHK How to fix KeyPress is not breaking the loop - scripting

The script is intended to break the loop on XButton1, but fails.
I am a newbie in AHK scripting and a really simple script I made is not working as intended. I googled it and it works on everyone.
ended = false
XButton1::
ended = true
return
$XButton2::
ended = false
Loop
{
if (ended = true)
{
break
}
MouseClick left
Sleep 10
}
return
It was supposed to click infinitely until Mouse4 (XButton1) is pressed. But it does not stop when I click it.
I also checked other StackOverflow posts and nothing solved it.

Your code uses legacy-syntax which was a major headache for me (and I believe many others) when I was starting with AHK.
To make your code work change:
if (ended = true)
to
if (ended = "true")
Consider switching to := (SetExpression) instead of =
For example:
ended = false
should become
ended := false

Related

AutoHotKey not finding image on a window

I recently discovered AutoHotKey, this scripting language seemed to be amazing !
Unfortunately, I can't manage to make my script find an image on a window (BlueStacks in my case).
Can someone please help me, my script is :
CoordMode, Pixel, screen
CoordMode, Mouse, screen
*ESC::ExitApp
ImgFound := false
while(ImgFound = false)
{
ImageSearch, ButtonX, ButtonY, 0, 0, A_ScreenWidth, A_ScreenHeight, *50 C:\Users\...\Documents\...\test.png
if (ErrorLevel = 2)
{
MsgBox Could not execute script.
ExitApp
}
else if (ErrorLevel = 1)
{
ImgFound := false
}
else
{
MsgBox Found.
click, %ButtonX%, %ButtonY%
ImgFound := true
}
}
Your while loop is unreachable code.
The code execution stops when the first hotkey label is encountered. This is called the Auto-execute Section.
Move your hotkey definition to be at the very bottom.
(All hotkeys always defined by hotkeys labels get always created regardless of being in the auto-execute section or not)

Groovy if statament

I'm new to Groovy and came to Groovy and grails from Java.
I cannot explain this:
boolean boolVar = false
if (boolVar) {
print "ok"
}
but code execution run inside "if" block, nut boolVar is false
debugger session screenshot
I think the screenshot is a bit wrong as you have nothing after the condition so intelliJ highlight this line but it does not execute it, it seems it goes to the block but it does not print the ok, can you show the result of the run window ?
for example add this to your code
boolean boolVar = false
def output = "I start here"
if (boolVar) {
output += "\noops in loop"
println "ok"
}
print output
only I start here is being printed
I'm not sure I get it by 100%, did you try this?
boolean boolVar = false
if (!boolVar) {
println "ok"
}
if(boolVar) and if(boolVar == true) is the same
1.) Remove all breakpoints from your project (Run->View Breakpoints and delete all breakpoints)
2.) Clean the target directory.
3.) Add a new breakpoint at the "print" statement and check if it works now.

Word2013 VBA disable spellchecking does not update screen

I have a Word macro, orginally recorded by performing the actions with user interface:
Options.CheckGrammarAsYouType = False
Options.CheckSpellingAsYouType = False
ActiveDocument.ShowGrammaticalErrors = False
ActiveDocument.ShowSpellingErrors = False
However , after executing the commands, Word keeps showing spell check errors. When disabled from user interface, the dissappear. Am I missing some kind of Refresh command ?
Add these codes, Document will get refreshed.
ActiveDocument.Application.ScreenRefresh
ActiveDocument.Application.ScreenUpdating = True

UltraWinGrid enter edit mode issue

I have an UltraWinGrid and I want to give the focus to a specific cell and make it enter edit mode programmatically (no click). So I did this :
If myUltraWinGrid.ActiveRow IsNot Nothing Then
myUltraWinGrid.ActiveCell = myUltraWinGrid.ActiveRow.Cells("foo")
myUltraWinGrid.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)
Else
myUltraWinGrid.ActiveCell = myUltraWinGrid.Rows(0).Cells("foo")
myUltraWinGrid.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)
EndIf
Which should work but it only gives focus to the row (no edit mode).
fooColumn
|
\_/
I don't know why but calling it with BeginInvoke solved the issue.
BeginInvoke(New MethodInvoker(AdressOf SetFocusToRow))
Private sub SetFocusToRow()
{
myUltraWinGrid.ActiveCell = myUltraWinGrid.ActiveRow.Cells("foo")
myUltraWinGrid.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)
}
Because it's executed by another thread by some reason
myUltraWinGrid.BeginInvoke(new MethodInvoker(()=> myUltraWinGrid.PerformAction(UltraGridAction.EnterEditMode)));

Best way to combine Play and Pause button?

this is more of an advise thread I guess.
I've been wondering how one could create a button which display "play" when it's not pressed. And then shows "pause" once it's pressed. And visa versa when it's pressed again.
I had a similar problem when trying to create an expand panel button, but that was easy because I could just set a variable to true or false if PanelCollapsed was true.
But in this case I couldn't find any property in a button that I could query.
So I came up with this but I can't help thinking that this is a rather unsmart way of doing it?
If isPlay = True Then
If isPaused = False Then
btnPlay.Image = Image.FromFile("iconPause.png")
isPaused = True
isPlay = False
End If
GoTo Endline
End If
If isPlay = False Then
If isPaused = True Then
btnPlay.Image = Image.FromFile("iconPlay.png")
isPaused = False
isPlay = True
End If
End If
Endline:
How about using only one variable and code like this:
If isPlay Then
btnPlay.Image = Image.FromFile("iconPause.png")
else
btnPlay.Image = Image.FromFile("iconPlay.png")
End If
isPlay = not isPlay
You can use the "Tag" property. Its type is "object" so you can use any object you want, but in your case a string will do:
If Button1.Tag = "Pause" Then
Button1.Image = Image.FromFile("iconPlay.png")
Button1.Tag = "Play"
Else
Button1.Image = Image.FromFile("iconPause.png")
Button1.Tag = "Pause"
End If
Most .NET WinForm controls have a 'Tag' property (a button has one). You can set the Tag to be anything you want. An easy way to do this is to set the 'Tag' property to a boolean with the state of the button.
Just an idea...sure there are many other approaches.
UPDATE:
Otherwise, you can maintain the state of the button in your application as its own member variable. This might have several advantages because you can pass this state to other controls that might need it. The only weakness with this approach is that the state must be maintained separately.
If you have a fairly straight-forward implementation, use the Tag property.
A contrary opinion ...
... while other answers have given you some techniques to achieve your desired result, I'm going to ask you to reconsider your UI design.
Dual state buttons - ones that alternate purpose when clicked - can be a source of user frustation.
Here are two scenarios.
Scenario #1 ... if the users machine is under load (for any reason), there may be a perceptible delay between the users actual click on your button and when your click handler is executed.
Normally the time between click and handler is a few milliseconds or less, but it can run to several seconds. If this happens when the user clicks on a dual state button, they are likely to click the button again. Net effect, when the application catches up, is to toggle on, then immediately off again.
Scenario #2 ... many users habitually double click everything. Even experienced users who've been using computers for years may have this weird habit. When they try to press a dual state button, guess what happens ... the action toggles on, then immediately off again.
There are at least two solutions ...
Solution #1 ... use two buttons, one for "On", one for "Off".
Solution #2 ... write some debouncing code to suppress the effect of a second click if processed immediately (ie: < 75ms) after the first.
I don't personally use Visual Basic, but I do know that Buttons in Windows Forms have a property called 'Tag'. It is of the generic object type, so you can save whatever state you want, and just use casting to get the value back out.
How about using the "Image" property?
Rem form initialization
ImagePlay = Image.FromFile("iconPlay.png")
ImagePause = Image.FromFile("iconPause.png")
Button1.Image = ImagePlay
.
.
.
Rem on button1 click
If Button1.Image = ImagePlay Then
Button1.Image = ImagePause
Else
Button1.Image = ImagePlay
End If