October 30, 2025, 05:32:12 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Counting wrong

Started by RitchieF, September 15, 2012, 03:56:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RitchieF

This little program is for counting purpose only.
Every time the user hits a defined key the counter should increase by one.
But depending on the key it increases by1 with a function key or by 2 with the RETURN-key or the SPACEBAR.

What's the reason for this ?

Thanks
Richard

window WIN
uint screenH,screenW
uint l,t,w,h
int counter = 1
GETSCREENSIZE screenW,screenH
OPENWINDOW Win,0,0,screenw,screenh-50,0x80C80080,0,"",&Win_handler
GETCLIENTSIZE win, l, t, w, h
CONTROL win,@RICHEDIT,"0",l,t,w,h,@CTEDITCENTER,1
SETFONT win, "Ariel", 500, 700, @SFITALIC,1
run = 1
WAITUNTIL run = 0
CLOSEWINDOW win
END
SUB Win_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW (win)

CASE @IDCLOSEWINDOW
CLOSEWINDOW win
run = 0
ENDSELECT
if GETKEYSTATE(0x7b) :' F12-key
'if GETKEYSTATE(0x0D) :' RETURN-key
'if GETKEYSTATE(0x20) :' SPACEBAR

SETCONTROLTEXT win,1,STR$(counter++)
endif

RETURN
ENDSUB

LarryMc

I wish I could give you some technical explanation that would blow your socks off but I can't.

Here's what I found out.
In the help file it says this about the GETKEYSTATE function.
QuoteReturn is greater than 0 if the key is currently being held down.
Since it didn't say "returns 1 if the key is currently being held down"

That made me curious so I looked at the source code for the GETKEYSTATE function.
It turns out it is simply a wrapper for the GetAsyncKeyState API function.

In the SDK help file it says:
QuoteThe GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
That really is not very straightforward to me.

I looked at your code again.
You have your GETKEYSTATE if statements outside the "normal" SELECT @MESSAGE structure.
That means you are checking the keystate everytime ANY message is sent which turns out to be a lot of messages.

So, I added the CASE @IDKEYUP and put the GETKEYSTATE if statements in there.
Now all keys increment the counter by 1.

Now it depends on what you are trying to accomplish as to whether or not this will work for you.

With your original code the counter would continue to increase if you held a key down (the repeat key feature).
That won't happen with my version.

Hope this helps a little.

window WIN
uint screenH,screenW
uint l,t,w,h
int counter = 1
GETSCREENSIZE screenW,screenH
OPENWINDOW Win,0,0,screenw,screenh-50,0x80C80080,0,"",&Win_handler
GETCLIENTSIZE win, l, t, w, h
CONTROL win,@RICHEDIT,"0",l,t,w,h,@CTEDITCENTER,1
SETFONT win, "Ariel", 500, 700, @SFITALIC,1
run = 1
WAITUNTIL run = 0
CLOSEWINDOW win
END
SUB Win_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW (win)

CASE @IDCLOSEWINDOW
CLOSEWINDOW win
run = 0
CASE @IDKEYUP
if GETKEYSTATE(0x7b)/*F12-key*/ or GETKEYSTATE(0x0D)/*RETURN-key*/ or GETKEYSTATE(0x20)/*SPACEBAR*/ then
SETCONTROLTEXT win,1,STR$(counter++)
endif
ENDSELECT
RETURN
ENDSUB


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

RitchieF

This helps me very much.
QuoteYou have your GETKEYSTATE if statements outside the "normal" SELECT @MESSAGE structure

I copied this from Egil's comport example where it worked.

So now I know how to handle GETKEYSTATE with the RETURN-key or the SPACEBAR.

Thanks again for your help

Richard

Andy

Hi,

Think you have it working now, this is a simple example that works for me.

'Compile and run - Press 'a' or 'x' to close.

DECLARE "user32.dll",TestKey alias GetAsyncKeyState(vKey:int),int
DECLARE "user32.dll",Pressed alias GetKeyState(vKey:int),int

OPENCONSOLE
PRINT
PRINT "Press the letter 'a' or 'x' to close."
PRINT

DO



'Letter 'A' test
IF TestKey(65) <> 0
IF Pressed(65) < 0
DO
UNTIL Pressed(65) >= 0
PRINT "Letter A pressed"
ENDIF
ENDIF

UNTIL INKEY$ = "x"
CLOSECONSOLE
END

Andy,
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

RitchieF

Thanks Andy for your code example.

It shows there are different ways to achieve the same result  :D

Richard


LarryMc

Quote from: RitchieF on September 16, 2012, 05:43:51 AM
It shows there are different ways to achieve the same result  :D
Apples and oranges I think.
The 1st is a way for a window/dialog and the 2nd for console mode.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library