Author Topic: One Key Input  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3663
    • Steve’s QB64 Archive Forum
One Key Input
« on: October 04, 2021, 12:55:45 PM »
For the Halloween Challenge that @bplus and @johnno56 have been talking about recently, one of the rules is to set up a program so that it only works with a single key being used.  For many folks, this could be a rather difficult thing to add into a game (How the heck do we get alphabetic responses with just the space bar??), so I thought I'd whip up a really simple little demo for one way to do this:

Code: (qb64) [Select]
$Color:0
Selection$ = "<Will be accepted after two seconds inactivity>": terminate = Timer: t = Timer

Do
    Cls
    k = _KeyDown(32)
    DisplayKeys Selection
    If t = 0 Then
        If k Then t = Timer: Selection = (Selection + 1) Mod 26
    Else
        If Timer > t + 2 Then
            Selection$ = Chr$(65 + Selection)
            t = 0
        Else
            If k Then t = Timer: Selection = (Selection + 1) Mod 26
            Selection$ = "<Will be accepted after two seconds inactivity>"
        End If
    End If
    If t <> 0 Then terminate = Timer
    Locate 5, 1: Print "SELECTED: "; Selection$; Chr$(13); "TERMINATION IN: "; _Ceil(terminate - Timer) + 5
    _Display
    _Limit 5
Loop Until Timer > terminate + 5 '5 seconds of zero activity after a selection will terminate the program completely.

Sub DisplayKeys (Selection)
    Color Red
    Locate Selection \ 13 + 1, (Selection Mod 13) * 3 + 2: Print "[ ]"
    Color BrightWhite
    For j = 0 To 1
        For i = 0 To 12
            Locate j + 1, (i + 1) * 3: Print Chr$(65 + 13 * j + i)
    Next i, j
End Sub

My whole little demo here comes in at around 30 lines of code or so, so something like this should be easy enough to implement in just about any sort of program someone would need it in.  The only real change might be a need to alter the termination part of the program (in this case it's just 5 seconds of total inactivity) to whatever suits your program needs instead.  (For example, hangman might terminate after the player either wins or dies...)

Usage...  Just press the SPACEBAR.  If you can't figure it out after that, I've did something wrong!   It's just a simple 1-key input routine!  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
Re: One Key Input
« Reply #1 on: October 04, 2021, 01:48:50 PM »
Yeah I suppose that would work and easier to understand than what I am using; it could all fit on one line too.
Just keep punching the spacebar until you're on your choice and then wait. Each punch might add to timer I imagine.
Oh verification would not be needed, just allot of spacebar punching.

Do suggest a different highlight color than dark red for that background, I couldn't even see it in semi cloudy afternoon here in Ohio.

Well, I guess I will rewrite this method into one function and install and test.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3663
    • Steve’s QB64 Archive Forum
Re: One Key Input
« Reply #2 on: October 04, 2021, 01:55:55 PM »
You can also hold down the spacebar to autoscroll, without having to beat the key to death.  The limit 5 will max increment 5 characters per second, so you should be able to choose the letter you desire within 5 seconds or so.

I imagine something like this would work real nice and easy for a Hangman-like game.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
Re: One Key Input
« Reply #3 on: October 04, 2021, 03:08:17 PM »
Quote
I imagine something like this would work real nice and easy for a Hangman-like game.

Yeah, it would work pretty nice for Game I have for challenge, here is Function I came up with:
Code: (qb64) [Select]
_Title "One Key System modified by bplus" ' b+ mod 2021-10-04
' 2021-10-04 Steve One Key System   https://www.qb64.org/forum/index.php?topic=4255.msg136324#msg136324
' 2021-10-04 bplus mod to a single Function for returning user input
Color 11, 0: Cls 'get black background
Do
    Print: Print "Select letter using spacebar to scroll to it: ";
    c$ = choice$(24, 25, " 1234ABCDEFGHIKKLMNOPQRSTUVWXYZ")
    here: If c$ <> " " Then Print c$ Else c$ = choice$(24, 25, " 1234ABCDEFGHIKKLMNOPQRSTUVWXYZ"): GoTo here:
Loop Until c$ = "Q"

Function choice$ (row, col, selection$)
    fg~& = _DefaultColor: bg~& = _BackgroundColor: place& = 0
    saveRow = CsrLin: saveCol = Pos(0): t = Timer
    GoSub show
    Do
        k$ = InKey$
        If k$ = Chr$(27) Then System ' emergency exit
        GoSub show:
        If k$ = " " Then
            t = Timer: place = (place + 1) Mod Len(selection$)
        Else ' watch out for midnight!
            If Timer - t > 4 Then choice$ = Mid$(selection$, place + 1, 1): Locate saveRow, saveCol: Exit Function
        End If
        _Limit 7 'so can hold down spacebar, nice
    Loop
    show:
    Locate row, col
    For i = 1 To Len(selection$)
        If i = place + 1 Then Color bg~&, fg~& Else Color fg~&, bg~&
        Locate row, col - 1 + i: Print Mid$(selection$, i, 1);
    Next
    Color fg~&, bg~&
    Return
End Function

I am leaving a space at the start of selections to signal the app that the user left to find Pete in the Submarine.

Offline johnno56

  • Forum Resident
  • Posts: 1166
  • Live long and prosper.
Re: One Key Input
« Reply #4 on: October 04, 2021, 04:18:44 PM »
Cool... One slight problem... Keyboard buffer may be a thing... Holding Space to scroll; Release... and the 'selector' keeps moving for a short time... I do not know the QB64 commands well enough to fix that... Moo Ha Ha....
Logic is the beginning of wisdom.

Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
Re: One Key Input
« Reply #5 on: October 04, 2021, 04:30:57 PM »
Cool... One slight problem... Keyboard buffer may be a thing... Holding Space to scroll; Release... and the 'selector' keeps moving for a short time... I do not know the QB64 commands well enough to fix that... Moo Ha Ha....

You could employ _KeyClear which clears keyboard buffer but I like trying to play shuffle board with the Spacebar, man it's a b when the you go slightly past the letter you want!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3663
    • Steve’s QB64 Archive Forum
Re: One Key Input
« Reply #6 on: October 04, 2021, 05:00:05 PM »
Cool... One slight problem... Keyboard buffer may be a thing... Holding Space to scroll; Release... and the 'selector' keeps moving for a short time... I do not know the QB64 commands well enough to fix that... Moo Ha Ha....

No keyboard buffer.  You’re not bothering to check if a series of keys were pressed.  All we need to know is if a _KEYDOWN even is occurring or not.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1773
Re: One Key Input
« Reply #7 on: October 04, 2021, 07:27:08 PM »
Well
I find fine your different proposals to get input using a line/table of the choices available (set of characters).
I have build this binary selection method that you can evaluate here
https://www.qb64.org/forum/index.php?topic=4257.msg136345#msg136345
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
Re: One Key Input
« Reply #8 on: October 09, 2021, 12:53:43 PM »
I have Steve's idea evolved to this in my code, free for anyone who wants to use in challenge and needs letters or menu choices by number up to 10 digits.

Code: (qb64) [Select]
_Title "One Key - Best so far Choice$" ' b+ 2021-10-09
' Best in my opinion One Key Control as date above


'setup for Choice$ function
Dim Shared KeyTimer, place ' preserve values between choice$ calls
KeyTimer = Timer ' <<<<<<<<< init outside main choice calling loop
Do
    Cls

    '
    ' Here we can run all the backgound graphics we want with no pauses waiting for input
    '

    Print "Use this One Key tool to Input your name or something using only spacebar!"
    Print "Select by spacing until highlite is over your choice letter or menu #."
    Print "Use Menu #1 at end of letters to signal the end of you input, like enter."
    Print "Use Menu #2 to actually insert a space in the text!"
    Print "Use escape to quit"
    Print
    Print "Progress: "; text$
    '''' KeyTimer = Timer ' <<<<<<<<<<<<<<<<<<<<<<<<<<<< no not just before calling choice, start outside loop
    c$ = choice$(10, 5, " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 ")
    If c$ <> "" And c$ <> " " And c$ <> "1" And c$ <> "2" Then text$ = text$ + c$
    If c$ = "1" Then
        Print "User input "; text$; ", resetting for another go."
        KeyTimer = Timer 'reset for another go round
        text$ = ""
        _Delay 3
    End If
    If c$ = "2" Then text$ = text$ + " "
    _Limit 30
Loop Until _KeyDown(27)

Function choice$ (row, col, selection$) ' One Key User Input System with tiny little setup
    ' setup  for best results space out selections$ for one letter at a time choice
    ' dim shared KeyTimer in main, set KeyTimer like KeyTimer = timer outside main loop with Choice$
    Dim As _Unsigned Long fg, bg
    Dim As Integer saveRow, saveCol, i
    Dim k$
    fg~& = _DefaultColor: bg~& = _BackgroundColor
    saveRow = CsrLin: saveCol = Pos(0)
    If _KeyDown(27) Then System ' emergency exit
    GoSub show:
    k$ = InKey$
    If k$ = " " Then KeyTimer = Timer: place = (place + 1) Mod Len(selection$)
    If Timer - KeyTimer >= 4 Then choice$ = Mid$(selection$, place + 1, 1): place = 0
    Locate saveRow, saveCol: Exit Function

    show:
    Locate row, col
    For i = 1 To Len(selection$)
        If i = place + 1 Then Color bg~&, fg~& Else Color fg~&, bg~&
        Locate row, col - 1 + i: Print Mid$(selection$, i, 1);
    Next
    '_Display  now the main loop handles since this is no longer a loop
    Color fg~&, bg~&
    Return
End Function

This does not cover the midnight problem with timer, who's up that late anymore? :)

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 812
  • At 60 I become highly radioactive!
Re: One Key Input
« Reply #9 on: October 12, 2021, 03:43:23 AM »
Why does this sound like some kind of Atari wana-be challenge? Hell they only gave you that one damn button.

Does mouse movement count? hover the mouse over something and use the 'one' button to activate that option.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
Re: One Key Input
« Reply #10 on: October 12, 2021, 09:42:35 AM »
Why does this sound like some kind of Atari wana-be challenge? Hell they only gave you that one damn button.

Does mouse movement count? hover the mouse over something and use the 'one' button to activate that option.

@Cobalt

Yes it does come from a Competition and discussion here at this forum and a Halloween Challenge for this forum that extends the specification for extra credit a Halloween Theme and/or screen 0 text, Console would count too I should think.
https://www.qb64.org/forum/index.php?topic=4245.msg136193#msg136193

Using the mouse positioning has already been ruled out, Steve brought it up maybe in the above link.
https://www.qb64.org/forum/index.php?topic=4245.msg136415#msg136415

Pete, Dav and I so far have posted code taking up the challenge. Pete stayed in Screen 0 (extra credit) and converted old code to Halloween Theme in link above, I think, Yes Here:
https://www.qb64.org/forum/index.php?topic=4245.msg136564#msg136564

Dav has a One key Halloween Game and has hinted at a bundle of them (graphics mode).
Dav's Jack Stack: https://www.qb64.org/forum/index.php?topic=4258.msg136358#msg136358

I am running a graphics version for Halloween and another Screen 0 variation on the Crypt-o-Gram Game in Text Mode: https://www.qb64.org/forum/index.php?topic=4258.msg136358#msg136358

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 812
  • At 60 I become highly radioactive!
Re: One Key Input
« Reply #11 on: October 12, 2021, 12:21:03 PM »
Well really haven't felt like programing lately, but I will give this a shot. Probably don't have the time but who knows.
Granted after becoming radioactive I only have a half-life!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1773
Re: One Key Input
« Reply #12 on: October 15, 2021, 05:35:15 AM »
One Key Challange
I have understood that the input system allowed was only Keyboard. No mouse No joystick.

about snippets posted here, they are very remarkable.
All 3 codes use the method
wait input, do while input, stop more than X time = finished
so you count time of waiting only at moment that input stops,
moreover until timeout, you can restart the choice procedure to change your selection.

I think that I'll turn my binary search including this time using method that is fastest to choose for all the humans (it lets remember me the Hall of Fame panel of cabinet videogames),
the previous one is good for other goals (just think when you play a videogame that let you choose the starting level from a panel).

Thanks for talking and sharing

PS
@SMcNeill
Steve , I know you are professional and expert.... why do you post an Old BASIC style snippet? With all those CHR$?
It is great but so poor flexible and poor reusable! How many code Should I change to show the alphabet in QWERTY order at the place of natural order?  :P


Programming isn't difficult, only it's  consuming time and coffee