How to return ascii value in 'bg _Kbd'

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
back_slash
Posts: 19
Joined: 20 Aug 2021 08:07

How to return ascii value in 'bg _Kbd'

#1 Post by back_slash » 04 Sep 2021 17:47

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?

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: How to return ascii value in 'bg _Kbd'

#2 Post by T3RRY » 04 Sep 2021 21:31

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

back_slash
Posts: 19
Joined: 20 Aug 2021 08:07

Re: How to return ascii value in 'bg _Kbd'

#3 Post by back_slash » 05 Sep 2021 06:38

@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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to return ascii value in 'bg _Kbd'

#4 Post by aGerman » 05 Sep 2021 09:36

Last time I used it the parameter was Kbd without leading underscore. Did you read the documentation?

Steffen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to return ascii value in 'bg _Kbd'

#5 Post by penpen » 05 Sep 2021 09:52

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

back_slash
Posts: 19
Joined: 20 Aug 2021 08:07

Re: How to return ascii value in 'bg _Kbd'

#6 Post by back_slash » 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.'

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: How to return ascii value in 'bg _Kbd'

#7 Post by T3RRY » 05 Sep 2021 12:12

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to return ascii value in 'bg _Kbd'

#8 Post by aGerman » 06 Sep 2021 02:20

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

back_slash
Posts: 19
Joined: 20 Aug 2021 08:07

Re: How to return ascii value in 'bg _Kbd'

#9 Post by back_slash » 06 Sep 2021 06:14

@aGerman & @penpen

Ah, thank you for explaining this to me.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to return ascii value in 'bg _Kbd'

#10 Post by Aacini » 07 Sep 2021 02:03

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

Post Reply