A Screen mode INKEY

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

A Screen mode INKEY

Post by eevee »

<B>Requirement:</B> It would be nice to have for Screen Modes the sort of functionality that INKEY() offers for Console Mode. This would allow one to trap keyboard characters whilst running say, a timer, as illustrated in the following thread:-

viewtopic.php?t=13625

<B>Suggested function:</B> KeyboardTest()

<B>Platform:</B> Windows and Linux

<B>Return:</B> True/False

Without experience, it is difficult to know what ExamineKeyboard() is doing. It would appear to flush out the keyboard buffer and then take a snapshot of the current pressed keys (if any). The problem seems to be that a following KeyboardInkey command, if used as a true/false test, does destroy buffer content as one would expect. Thus it does not seem possible to KeyboardInkey again immediately, this time with the intention of grabbing the character. This is indicated in the thread mentioned above.

This proposal then, is for a similar routine to KeyboardInkey that does not destroy the keyboard buffer. It would thus allow:-

Code: Select all

Repeat

achar$=""
If KeyboardTest()
    achar$=KeyboardInkey()
Else
    Test the timer
    If Timeout
	achar$="Telltale"
    EndIf
EndIf
		
Until achar$<>""
Now one can route depending on whether achar$ is the telltale or a character.[/code]
Regards,

Ernest
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

can't you detect keyboard events or use an api to get the state of specific keys? it's slightly more cumbersome, i do admit that...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

Post by eevee »

I'm sure you are right.

However it seems a pity that a primitive function such as INKEY should not exist or be properly implemented in a lot of 'modern' languages.

Regarding your comments, I cannot as yet test API types because I am running the demo version of PureBasic.

Keyboard events? I had never thought of this so will look into it further. However ideally I need to keep everything very simple since the target system will have no GUI, no mouse, reduced 8-key keyboard and more.

It is because of this that the final destination will be Linux. In other words I guess I am trying to get a 'modern' pretend DOS system going!
Regards,

Ernest
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

Post by eevee »

As promised earlier, I have now taken a look at keyboard events.

The following code works well and does give me the required function, that is, a returned character from the keyboard or a timeout.

Code: Select all

    itime.b
    anitem.b
  
;Initialising the window And events
If OpenWindow(0,0,0,0,0,#PB_Window_ScreenCentered|#PB_Window_BorderLess," ") 
    AddKeyboardShortcut(0, #PB_Shortcut_1, 1) 
    AddKeyboardShortcut(0, #PB_Shortcut_2, 2) 
    AddKeyboardShortcut(0, #PB_Shortcut_3, 3) 
    AddKeyboardShortcut(0, #PB_Shortcut_4, 4)

;Test routine proper
    itime=5                                             ;Set a test timeout    
    While anitem<>5
        Secs = Date()                                   ;Get current date in seconds .....
        Secs=Secs + itime                               ;..... and add the required timeout
        anitem=0
        Gosub CHARIN
        MessageRequester("Result","It was a "+ Str(anitem), 0)
    Wend
EndIf 
   
End


;Routine returns a pre-defined keyboard character or 5 if timed out.
CHARIN:

    Repeat 
        EventID = WindowEvent() 

        If EventID=#PB_Event_Menu 
            anitem=EventMenuID()
        Else
            Atime = Date()                              ;Get the date in seconds
            If Atime>Secs                               ;Test For timeout
                anitem = 5                              ;Force timeout
            EndIf
        EndIf
    Until anitem<>0
Return
Notice that I have reduced the window to nothing in size (invisible does not work).

Question is, can one trap window events from full screen mode?

To stick to a windows mode rather than a screen mode does seem to add unneccesary complications in having to set the window to fill the screen, removal of excesses, background colouring etc.. Other threads seem to suggest this is quite convoluted when all one wants to do is show a series of static pictures with simple text.
Regards,

Ernest
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

Post by eevee »

I want to thank you for your suggestion which led to my previous posting, and so to a solution (I think).

Rather than clutter this request thread I have put the code on the thread that originated my plea for help.

Here it is:-
viewtopic.php?t=13625

I think perhaps, if my INKEY suggestion did exist, it certainly would have saved me about four days fiddling! Still I guess I am learning a lot about PureBasic and its way of doing things!

Once again, many thanks.
Regards,

Ernest
Post Reply