Page 1 of 1
How to return ascii value in 'bg _Kbd'
Posted: 04 Sep 2021 17:47
by back_slash
I'm using BG.EXE 2.5 by @carlos on Windows 10
My code is
Code: Select all
@echo off
echo Press any key.
bg.exe _Kbd
timeout 3 /nobreak >nul
echo %ERRORLEVEL%
pause
exit
But when I press a key, it doesn't return a value in ERRORLEVEL, instead 0.
How do I fix this?
Re: How to return ascii value in 'bg _Kbd'
Posted: 04 Sep 2021 21:31
by T3RRY
using timeout after bg.exe is resulting in the errorlevel returning to 0.
Store or assess the returned errorlevel before enacting other commands.
Note : the latest version of Carlos' Batch Graphics, including full documentation, can be found at
https://github.com/carlos-montiers/cons ... /master/bg
Re: How to return ascii value in 'bg _Kbd'
Posted: 05 Sep 2021 06:38
by back_slash
@T3RRY
Well, I have tried this code which stores the ERRORLEVEL in a different variable, but it still returns 0.
Code: Select all
@echo off
echo Press any key.
bg.exe _Kbd
set key=%ERRORLEVEL%
timeout 3 /nobreak >nul
echo %key%
pause
exit
I've also tried 'bg sleep 3000' instead of timeout and it still doesn't work
Any solutions?
Re: How to return ascii value in 'bg _Kbd'
Posted: 05 Sep 2021 09:36
by aGerman
Last time I used it the parameter was Kbd without leading underscore. Did you read the documentation?
Steffen
Re: How to return ascii value in 'bg _Kbd'
Posted: 05 Sep 2021 09:52
by penpen
The parameter "_Kbd" commands the bg.exe tool to read a character from the input buffer if present, else returns 0.
As this is clearly not what you (back_slash) are looking for, you should follow Steffen's suggestion to use "Kbd" instead.
penpen
Re: How to return ascii value in 'bg _Kbd'
Posted: 05 Sep 2021 11:03
by back_slash
@aGerman
I have. On this post (
viewtopic.php?t=4094) it states there is an option called "_Kbd" to wait until a key was pressed.
@penpen
The reason I'm using _Kbd is to wait 3 seconds to give you time to press a key, The issue with Kbd is you have to press a key instead of it being optional.
Here's an example:
'Press any key in 5 seconds, if not set pressed=false.'
Re: How to return ascii value in 'bg _Kbd'
Posted: 05 Sep 2021 12:12
by T3RRY
back_slash wrote: ↑05 Sep 2021 11:03
@aGerman
I have. On this post (
viewtopic.php?t=4094) it states there is an option called "_Kbd" to wait until a key was pressed.
@penpen
The reason I'm using _Kbd is to wait 3 seconds to give you time to press a key, The issue with Kbd is you have to press a key instead of it being optional.
Here's an example:
'Press any key in 5 seconds, if not set pressed=false.'
_Kbd
::If any key was pressed, returns the ascii code, else return 0.
::If the key pressed is extended, it returns the second code plus 255. Example. If you pressed the up arrow (224, 72) returns 327 (72+255).
Notice "was" not "is"
_kbd flags to use a mode theat reads the buffer to see if a key has been pressed. This will only result in a non zero errorlevel if a key was pressed BEFORE bg.exe _kbd is used and the input buffer has not yet been cleared.
To use bg.exe _kbd to wait for a new keypress without it blocking script execution until a key is pressed, you need to use it in a loop.
Break the loop using goto after a non zero errorlevel is returned or after a set amount of time has passed using a timeelapsed calculcation.
I recommend you look at the archive for bg.exe 3.9 I linked to previously, specifically the example folder and example_1.bat
A method has been exampled here - Note:
_kbd has been updated to:
lastkbd
Re: How to return ascii value in 'bg _Kbd'
Posted: 06 Sep 2021 02:20
by aGerman
Yes, T3RRY is right.
Untested:
Code: Select all
set /a "n=0"
:loop
set /a "n+=1"
bg.exe sleep 100
bg.exe _Kbd
if not errorlevel 1 if %n% lss 30 goto loop
echo %errorlevel%
It checks every 100ms if a key is pressed. Runs max. 30 iterations. So, in the end ~3s. Errorlevel should be 0 if no key was pressed in this time slot.
Steffen
Re: How to return ascii value in 'bg _Kbd'
Posted: 06 Sep 2021 06:14
by back_slash
@aGerman & @penpen
Ah, thank you for explaining this to me.
Re: How to return ascii value in 'bg _Kbd'
Posted: 07 Sep 2021 02:03
by Aacini
You may also use my GetKey.exe program this way:
Get a key from keyboard and return its value in ERRORLEVEL.
GetKey [/N]
Ascii characters are returned as positive values, extended keys as negative values.
If /N switch is given, no wait for a key: immediately return zero if no key was pressed.
You may download GetKey.exe file (and other useful auxiliary .exe programs) from
Advanced Batch features via auxiliary .exe programs.
Antonio