Posts: 171
Threads: 17
Likes Received: 53 in 42 posts
Likes Given: 59
Joined: Oct 2023
Reputation:
3
This is a little confusing, but here goes!
I have a program that I want to rerun for a second or more times. So, I clear the GraphicsWindow, and tell it to rerun by using a button. The problem I am having is: in the first run I use this:
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
This is to hold the program until I hit the return key. That works just fine. Then steps in "GraphicsWindow.LastKey" That function is read only. The problem is there is no way to reset it in standard "Small Basic" and it remembers the key was used in the first round of the program. So, on the second round the program zips right through the while loop. To work properly GraphicsWindow.LastKey needs to reset for the second or more rounds of the program. I looked in the LitDev Extension. Didn't see any way to reset it.
Any suggestions of how to do it?
Maybe, LitDev could add it to the extension?
JR
Posts: 498
Threads: 38
Likes Received: 405 in 278 posts
Likes Given: 194
Joined: Aug 2023
Reputation:
18
01-15-2025, 02:09 PM
(This post was last modified: 01-15-2025, 02:17 PM by litdev.)
Depending on what your program actually does this may not be the best approach, but the following may work for you. Others may have different approaches, maybe LDUtilities.KeyDown, GraphicsWindow.KeyDown event - a challenge to find different ways to write WaitForReturnKey subroutine?
Code: GraphicsWindow.Show()
While ("True")
WaitForReturnKey()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetRandomColor()
EndWhile
Sub WaitForReturnKey
key = "" 'Make sure key isn't initially Return before LastKey sets it
LDTextWindow.SendKey(GraphicsWindow.Title,"Space") 'Send a Space key so initially LastKey is not Return
While key <> "Return"
key = GraphicsWindow.Lastkey
Program.Delay(10) 'Don't mash cpu
EndWhile
EndSub
Posts: 171
Threads: 17
Likes Received: 53 in 42 posts
Likes Given: 59
Joined: Oct 2023
Reputation:
3
LitDev,
In my program I put the following:
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
LDTextWindow.SendKey(GraphicsWindow.Title,"Space") 'Send a Space key so initially LastKey is not Return
key=GraphicsWindow.LastKey
TW.WriteLine(key)
GraphicsWindow.LastKey still thinks it was the "Return" key
Then I tried just this:
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
TextWindow.WriteLine("H")
When I run this and hit the return key it doesn;t do the TW. Any idea?
Posts: 498
Threads: 38
Likes Received: 405 in 278 posts
Likes Given: 194
Joined: Aug 2023
Reputation:
18
01-15-2025, 03:18 PM
(This post was last modified: 01-15-2025, 03:19 PM by litdev.)
Hi, interesting.
For the first one, GW needs to be present and it seems that processessing of the key takes some time to fire and register async events by the OS, so some delays let it filter through.
Code: GraphicsWindow.Show()
While key <> "Return"
key = GraphicsWindow.Lastkey
Program.Delay(10)
EndWhile
LDTextWindow.SendKey(GraphicsWindow.Title,"Space") 'Send a Space key so initially LastKey is not Return
Program.Delay(10)
key=GraphicsWindow.LastKey
TW.WriteLine(key)
For second one, it works for me when GW.Show() is used.
Code: GW.Show()
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
TextWindow.WriteLine("H")
Posts: 171
Threads: 17
Likes Received: 53 in 42 posts
Likes Given: 59
Joined: Oct 2023
Reputation:
3
01-15-2025, 03:58 PM
(This post was last modified: 01-15-2025, 04:15 PM by jrmrhrb00.
Edit Reason: Edit
)
LitDev,
This snippet works now. I agree. "Strange"
GW.Show()
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
TextWindow.WriteLine("H")
I am pretty sure in my program that the LDTextWindow.SendKey doesn't reset GraphicsWindow.LastKey. I put a TW.Writeline(GraphicsWindow.LastKey in front of the wait loop and for the second run of the program it shows the "Return" key. It should show the space key, but doesn't.
Would it be possible in the extension under utilities to add a reset function to GraphicsWindow.LastKey?
JR
Here is what I have in the actual program:
'TW.WriteLine(GraphicsWindow.LastKey)
While key <> "Return"
key = GraphicsWindow.Lastkey
EndWhile
Program.Delay(10)
LDTextWindow.SendKey(GraphicsWindow.Title,"Space") 'Send a Space key so initially LastKey is not Return
Program.Delay(10)
key=GraphicsWindow.LastKey
TW.WriteLine(key)
It never shows "Space as the last key. Only "Return"
Posts: 498
Threads: 38
Likes Received: 405 in 278 posts
Likes Given: 194
Joined: Aug 2023
Reputation:
18
Please try the subroutine method which sets key and Lastkey before the loop, you don't appear to be doing this.
This works every time for me, so I feel there may be something else going on in your program and would like to understand that first. There are other ways also to just wait for a Return key press as I suggested so don't want to add a 'weird hack' feature to change something that I think may be covering up some other issue and could get you or others into other problems.
Can you share an actual (simplified if possible) program that shows the issue I can reproduce, using the WaitForReturnKey subroutine below.
Code: GraphicsWindow.Show()
While ("True")
WaitForReturnKey()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetRandomColor()
EndWhile
Sub WaitForReturnKey
key = "" 'Make sure key isn't initially Return before LastKey sets it
LDTextWindow.SendKey(GraphicsWindow.Title,"Space") 'Send a Space key so initially LastKey is not Return
Program.Delay(10)
While key <> "Return"
key = GraphicsWindow.Lastkey
Program.Delay(10) 'Don't mash cpu
EndWhile
EndSub
Another way to do it:
Code: GraphicsWindow.Show()
GraphicsWindow.KeyDown = OnKeyDown
While ("True")
WaitForReturnKey()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetRandomColor()
EndWhile
Sub WaitForReturnKey
key = ""
While (key <> "Return")
Program.Delay(10)
EndWhile
EndSub
Sub OnKeyDown
key = GraphicsWindow.LastKey
EndSub
And another:
Code: GraphicsWindow.Show()
While ("True")
WaitForReturnKey()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetRandomColor()
EndWhile
Sub WaitForReturnKey
While (LDUtilities.KeyDown("Return") <> "True")
Program.Delay(10)
EndWhile
EndSub
Posts: 171
Threads: 17
Likes Received: 53 in 42 posts
Likes Given: 59
Joined: Oct 2023
Reputation:
3
LitDev,
Here's the program. GPVC238.000. Once started. Type the number of seconds you want it to run. When it finishes you will see a box for "again" click it. The problem is it runs straight thru. If you look at the TW it will show the return key twice. The second time thru it should stop in the wait loop to allow for inputting seconds.
JR
Posts: 498
Threads: 38
Likes Received: 405 in 278 posts
Likes Given: 194
Joined: Aug 2023
Reputation:
18
JR,
Really nice program and I can reproduce your issue.
So first I replaced your return check code with my subroutine and still see the issue.
So then I noticed that the second time Test is called from inside OnButtonClick - this is a non-UI event thread, and Test does struff that should be on the main UI thread, so I updated your code for this and still see the issue!
Next I realised that the TextBox has focus and the Space key is being sent there and the GraphicsWindow.LastKey remains as "Return" since the Space key event didn't get passed to the GraphicsWindow, it just went to the TextBox. Then I removed the TextBox focus and it worked, but you do need to click inside the textbox to enter a number to it. It is interesting that GW.LastKey does get the key click in the TextBox (this event bubbles up), but GW does not get the Space key. Often once an event is handled it then does not progress up the visual tree to a parent element, so the TextBox says I've used the Space key and no need for any other window elements to get this event.
This the code: WMXJ902.000
And here is version where the TextBox focus is set after the Space has been sent to the GW, this also works: WMXJ902.000-0
Fun little puzzle!
Posts: 171
Threads: 17
Likes Received: 53 in 42 posts
Likes Given: 59
Joined: Oct 2023
Reputation:
3
01-15-2025, 07:56 PM
(This post was last modified: 01-15-2025, 08:03 PM by jrmrhrb00.
Edit Reason: Edit
)
LitDev,
You are so good at this stuff. I have spent days, and you have spent minutes. Amazing! I really didn't want to give you the program as it was for the hourglass challenge. Now, I'm glad that I did because I would have never figured it out!
So, with the LDTextWindow.SendKey(GraphicsWindow.Title,"Space" the GraphicsWindow has to have focus and the problem was I had it on the textbox? So, I know I can remove it from there, but how do you get it back to the GraohicsWindow? You can only set focus on shapes. Originally I never even thought about the LDTextWindow.SendKey function. Learn something new every day!
Thaks for helping! You should be a teacher.
JR
Posts: 498
Threads: 38
Likes Received: 405 in 278 posts
Likes Given: 194
Joined: Aug 2023
Reputation:
18
I don't think there is currently a way to set focus on GW, but there are other ways to get the Return key without Lastkey/SendKey. Here is another one:
Code: GraphicsWindow.Show()
mtb = Controls.AddMultiLineTextBox(10,10) 'Multi-line will allow return
Controls.SetSize(mtb,100,21) 'Size to show 1 line only, just big enough to not need vertical scroll bar
LDFocus.SetFocus(mtb)
While ("True")
WaitForReturnKey()
GraphicsWindow.BackgroundColor = GraphicsWindow.GetRandomColor()
EndWhile
Sub WaitForReturnKey
char = ""
While (char <> Text.GetCharacter(10)) 'Ascii 10 is LF (Return)
tb = Controls.LastTypedTextBox
text = Controls.GetTextBoxText(tb)
len = Text.GetLength(text)
char = Text.GetSubText(text,len,1) 'last character will be LF if Return was pressed
Program.Delay(10)
EndWhile
Controls.SetTextBoxText(tb,"") 'Clear text
EndSub
|