Moving a 2d circle using keyboard example

New to FreeBASIC? Post your questions here.
fxm
Moderator
Posts: 12396
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Moving a 2d circle using keyboard example

Post by fxm »

I did not say it was impossible, I just said it was not recommended for a beginner like Briscoedjs:
The keyboard input buffer is not disabled while you use MultiKey; that is, pressed keys will be stored and subsequently returned by your next call to Inkey or GetKey or Input. This means you have to empty the keyboard input buffer manually when you finish using MultiKey, using something like the following method:
While Inkey <> "": Wend '' loop until the keyboard input buffer is empty

Keeping Inkey to work while you use MultiKey allows more flexibility and can be useful to detect Chr(255)+"k" combo returned on window close button click, if a windowed graphics mode has been set via the Screen (Graphics) statement.
Jattenalle
Posts: 61
Joined: Nov 17, 2023 14:41
Contact:

Re: Moving a 2d circle using keyboard example

Post by Jattenalle »

fxm wrote: Aug 31, 2024 14:12 I did not say it was impossible, I just said it was not recommended for a beginner like Briscoedjs:
Are you gaslighting me right now? Because that's not what you said. You said, in its entirety:
To close the graphic window with its close button, you have to detect the sequence: Chr(255) + "k", or: Chr(255, Asc("k"))
Multikey does not work with the close button, but only Inkey.
Since it is not recommended to use Multikey and Inkey simultaneously, I have rewritten the code to use only Inkey:
(Emphasis added)
And regardless of if you supposedly meant something other than what you wrote, you're still wrong. Using multikey for movement is exactly what you would use it for.

Inkey and multikey do very different things. You never want to use inkey for user input that is supposed to react to keys being held (e.g. continuous movement)
And, you never want to use multikey for things that should only react when the user input is a press (e.g. pressing escape, opening inventory, etc)
There is literally no reason why you can't use both at the same time, for their intended uses: multikey if you want to see if something is held, and inkey if you want to see if something was pressed.
fxm wrote: Aug 31, 2024 14:12
The keyboard input buffer is not disabled while you use MultiKey; that is, pressed keys will be stored and subsequently returned by your next call to Inkey or GetKey or Input. This means you have to empty the keyboard input buffer manually when you finish using MultiKey, using something like the following method:
While Inkey <> "": Wend '' loop until the keyboard input buffer is empty

Keeping Inkey to work while you use MultiKey allows more flexibility and can be useful to detect Chr(255)+"k" combo returned on window close button click, if a windowed graphics mode has been set via the Screen (Graphics) statement.
Yes, they do different things. Why would multikey do anything to the inkey buffer? You're the one who claim it's not recommended to use both at the same time.

And the proper solution, as opposed to that absolute 'buffer clearing' nonsense, is to just have an inkey in your main input loop.
This works perfectly fine, as expected, without any weird forced buffer clearing. And with both multikey and inkey working at the same time.

Code: Select all

#include once "fbgfx.bi"
using fb

screenres 320,200,32,2

dim as integer c
dim as string userKey
do
	userKey = inkey
	
	if multikey(SC_PLUS) then
		c += 1
	end if
	if userKey = "-" then
		c -= 1
	end if
	
	locate 1,1: print "Press + to increase with multikey,"
	print "press - to decrease with inkey"
	print "ESC to exit"
	locate 5,10: print c
	flip
loop until multikey(SC_ESCAPE) or userKey = chr(27)
hhr
Posts: 250
Joined: Nov 29, 2019 10:41

Re: Moving a 2d circle using keyboard example

Post by hhr »

The example works with the + of the numeric keypad, not with the + of the main keyboard. Should my keyboard be broken?
fxm
Moderator
Posts: 12396
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Moving a 2d circle using keyboard example

Post by fxm »

With Multikey you can detect 'Shift' And '=', but on the other hand the state of 'Caps Lock' activated is not reported.
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Moving a 2d circle using keyboard example

Post by dodicat »

Using inkey and screenevent

Code: Select all

#include once "fbgfx.bi"
using fb
dim x as event

screenres 1024,200,32,2
screenset 1,0


dim as single key

dim as integer c=512
dim as string userKey
do
    screenevent(@x)
    cls
	userKey = inkey
    select case userkey
    case "+"
            key+=1
            c+=key
    case "-"
        key+=1
        c-=key
    end select
    '==============
    if x.type=EVENT_KEY_RELEASE then key=0
    if c>1024 or c<0 then c=512:key=0
    '==============
	circle(c,100),10,,,,,f
	locate 1,1: print "Press + to increase with multikey,"
	print "press - to decrease with inkey"
	print "ESC to exit"
	locate 5,10: print c
  
	flip
   
loop until  userKey = chr(27)  
Here using main keyboard + does nothing also with multikey
hhr
Posts: 250
Joined: Nov 29, 2019 10:41

Re: Moving a 2d circle using keyboard example

Post by hhr »

Code: Select all

dim as ubyte i
do
   if multikey(i) then print i, hex(i)
   i += 1
loop
With this example I tested multikey. It turned out that + of the main keyboard is &h0D (SC_EQUALS).

In the example of Jattenalle I can change line 11:

Code: Select all

if multikey(SC_PLUS) or multikey(SC_EQUALS) then
What does SC_EQUALS mean?
SARG
Posts: 1846
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Moving a 2d circle using keyboard example

Post by SARG »

hhr wrote: Sep 01, 2024 9:07 What does SC_EQUALS mean?
With my french keyboard "+" and "=" are the same key. "+" is gotten when SHIFT key is also pressed.
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Moving a 2d circle using keyboard example

Post by dodicat »

I can get it to work with:

Code: Select all

#include once "fbgfx.bi"
using fb

screenres 320,200,32,2
screenset 1,0
dim as integer c,flag
dim as string userKey
do
	userKey = inkey
	
	if (multikey(SC_EQUALS) and multikey(SC_LSHIFT) and multikey(SC_RSHIFT)) and flag=1 then
		c += 1
        flag=0
	end if
	if userKey = "-" then
		c -= 1
	end if
	
	locate 1,1: print "Press + to increase with multikey,"
	print "press - to decrease with inkey"
	print "ESC to exit"
	locate 5,10: print c
    flag=len(userkey)
	flip
loop until multikey(SC_ESCAPE) or userKey = chr(27) 
Note I used a flag to stop multikey at each keypress so it behaves like userkey = "-"
Win 11, Linux might be different, or indeed different countries with different keyboards.
Like SARG, here = and + are on the same key, and shift must be pressed to get +
hhr
Posts: 250
Joined: Nov 29, 2019 10:41

Re: Moving a 2d circle using keyboard example

Post by hhr »

Now I understand, thanks. The German keyboard is a bit misleading.
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Moving a 2d circle using keyboard example

Post by dodicat »

The loop bit of this is by fxm a few years ago.
Just briefly touch keys.

Code: Select all

function ret(n as long) as string
    dim as string s
    select case n
    
case &h01: s=  "SC_ESCAPE       "
case &h02: s=  "SC_1            "
case &h03: s=  "SC_2            "
case &h04: s=  "SC_3            "
case &h05: s=  "SC_4            "
case &h06: s=  "SC_5            "
case &h07: s=  "SC_6            "
case &h08: s=  "SC_7            "
case &h09: s=  "SC_8            "
case &h0A: s=  "SC_9            "
case &h0B: s=  "SC_0            "
case &h0C: s=  "SC_MINUS        "
case &h0D: s=  "SC_EQUALS       "
case &h0E: s=  "SC_BACKSPACE    "
case &h0F: s=  "SC_TAB          "
case &h10: s=  "SC_Q            "
case &h11: s=  "SC_W            "
case &h12: s=  "SC_E            "
case &h13: s=  "SC_R            "
case &h14: s=  "SC_T            "
case &h15: s=  "SC_Y            "
case &h16: s=  "SC_U            "
case &h17: s=  "SC_I            "
case &h18: s=  "SC_O            "
case &h19: s=  "SC_P            "
case &h1A: s=  "SC_LEFTBRACKET  "
case &h1B: s=  "SC_RIGHTBRACKET "
case &h1C: s=  "SC_ENTER        "
case &h1D: s=  "SC_CONTROL      "
case &h1E: s=  "SC_A            "
case &h1F: s=  "SC_S            "
case &h20: s=  "SC_D            "
case &h21: s=  "SC_F            "
case &h22: s=  "SC_G            "
case &h23: s=  "SC_H            "
case &h24: s=  "SC_J            "
case &h25: s=  "SC_K            "
case &h26: s=  "SC_L            "
case &h27: s=  "SC_SEMICOLON    "
case &h28: s=  "SC_QUOTE        "
case &h29: s=  "SC_TILDE        "
case &h2A: s=  "SC_LSHIFT       "
case &h2B: s=  "SC_BACKSLASH    "
case &h2C: s=  "SC_Z            "
case &h2D: s=  "SC_X            "
case &h2E: s=  "SC_C            "
case &h2F: s=  "SC_V            "
case &h30: s=  "SC_B            "
case &h31: s=  "SC_N            "
case &h32: s=  "SC_M            "
case &h33: s=  "SC_COMMA        "
case &h34: s=  "SC_PERIOD       "
case &h35: s=  "SC_SLASH        "
case &h36: s=  "SC_RSHIFT       "
case &h37: s=  "SC_MULTIPLY     "
case &h38: s=  "SC_ALT          "
case &h39: s=  "SC_SPACE        "
case &h3A: s=  "SC_CAPSLOCK     "
case &h3B: s=  "SC_F1           "
case &h3C: s=  "SC_F2           "
case &h3D: s=  "SC_F3           "
case &h3E: s=  "SC_F4           "
case &h3F: s=  "SC_F5           "
case &h40: s=  "SC_F6           "
case &h41: s=  "SC_F7           "
case &h42: s=  "SC_F8           "
case &h43: s=  "SC_F9           "
case &h44: s=  "SC_F10          "
case &h45: s=  "SC_NUMLOCK      "
case &h46: s=  "SC_SCROLLLOCK   "
case &h47: s=  "SC_HOME         "
case &h48: s=  "SC_UP           "
case &h49: s=  "SC_PAGEUP       "
case &h4B: s=  "SC_LEFT         "
case &h4D: s=  "SC_RIGHT        "
case &h4E: s=  "SC_PLUS         "
case &h4F: s=  "SC_END          "
case &h50: s=  "SC_DOWN         "
case &h51: s=  "SC_PAGEDOWN     "
case &h52: s=  "SC_INSERT       "
case &h53: s=  "SC_DELETE       "
case &h57: s=  "SC_F11          "
case &h58: s=  "SC_F12          "

'' Extra scancodes not compatible with DOS scancodes
case &h5B: s=  "SC_LWIN         "
case &h5C: s=  "SC_RWIN         "
case &h5D: s=  "SC_MENU         "
end select
return s
end function

screen 19
Print "<Esc> to exit othewise test the keyboard"
Print
'This bit by fxm 
Do
    Dim K As Integer
    For I As Integer = 0 To 127
        If Multikey(I) Then
            If K = 0 Then
                K = 1
            Else
                Print " And   ";
            End If
            Print ret(i);
            If I = 1 Then
                Exit Do
            End If
        End If
    Next I
    If K = 1 Then
        Print
        K = 0
    End If
    Sleep 500
Loop
While Inkey <> ""
Wend
Print
Print
Print "Any key to exit."
Sleep 
For gaming probably the arrows are the best choice for multikey in general.
Briscoedjs
Posts: 18
Joined: Apr 16, 2022 4:38

Re: Moving a 2d circle using keyboard example

Post by Briscoedjs »

Thanks everyone for your detailed replies.
Post Reply