Page 1 of 1

Keyboard input

Posted: Mon May 02, 2022 4:28 pm
by pfaber11
Hello. I'm having a problem with the keyboard in PB . It works fine for everything else .
This is what I'm doing .

Code: Select all

ExamineKeyboard()
If KeyboardPushed(#PB_Key_D)
  End
  Endif
  
I have initkeyboard() earlier in the program.
is there some thing I am not aware of .
I'm using PureBasic 5.73
I'm on a hp laptop 16gb 128gb and upto 3.4 ghz
Any ideas
Thanks for reading

Re: Keyboard input

Posted: Mon May 02, 2022 6:59 pm
by Demivec
Your post doesn't describe the problem in any detail nor does it include runable code that demonstrates the problem.

Based on the previous facts, here's a guess:
Are you using a screen and executing FlipBuffers() between calls to ExamineKeyboard() so that the readings can be updated?

Re: Keyboard input

Posted: Tue May 03, 2022 11:18 am
by pfaber11
I'm going to post the whole program so you can see it . The problem is this when I run my program the keyboard does not work for if keyboard pushed.
here is my code . By the way I'm new to purebasic but not to BASIC in general.

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()
OpenScreen(1,1,32,"take1")
OpenWindow(5,50,50,600,500,"take2")

SetFrameRate(60)
SetWindowColor(5,RGB(0,250,0))
RenderWorld()
timer1=1
timer2=24
starter:
timer1=timer1+1
timer2=timer2+1

StartDrawing(WindowOutput(5))
DrawText(timer1,timer2,"hello world",#Red,RGB(0,250,0))
DrawText(325,275,"excel",#Red,#Green)
StopDrawing()

If timer1>= 599
  timer1=1
  EndIf

  If timer2>=499
    timer2=1
  EndIf  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_H)
    CloseWindow(5)
    CloseScreen()
  EndIf
  If WindowEvent()=#PB_Event_CloseWindow
    CloseWindow(5)
    CloseScreen()
  EndIf 
 FlipBuffers()
Goto starter
I even formatted and reinstalled windows 11 to no avail, Thanks for your time.

Re: Keyboard input

Posted: Tue May 03, 2022 1:00 pm
by Demivec
Welcome to the forums and PureBasic pfaber11.

Here's your slightly modified code:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

;OpenScreen(1,1,32,"take1")
OpenWindow(5,50,50,600,500,"take2")
OpenWindowedScreen(WindowID(5), 0, 0, 1, 1)
SetFrameRate(60)
SetWindowColor(5,RGB(0,250,0))
RenderWorld()
timer1=1
timer2=24

;starter:
Repeat
  timer1=timer1+1
  timer2=timer2+1
  
  StartDrawing(WindowOutput(5))
  DrawText(timer1,timer2,"hello world",#Red,RGB(0,250,0))
  DrawText(325,275,"excel",#Red,#Green)
  StopDrawing()
  
  Repeat
    event = WindowEvent()
    If WindowEvent()=#PB_Event_CloseWindow
      CloseWindow(5)
      CloseScreen()
      End
    EndIf 
  Until event = 0
  
  If timer1>= 599
    timer1=1
  EndIf
  
  If timer2>=499
    timer2=1
  EndIf  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_H)
    CloseWindow(5)
    CloseScreen()
    End
  EndIf
 
 Delay(1) ;turn over remaining time slice for other programs running on OS
  FlipBuffers()
  ;Goto starter
ForEver
I addressed several issues.

The first is you open a screen and a window at the same time. A screen is full-screen. Because it is full-screen it much be open with the resolution of a supported screen size. I'm pretty sure 1x1 is not a supported size. I changed the code to open a windowed screen on the window you opened. The window screen can be any size less than or equal to the size of the window it is on, so 1x1 works fine (though useless at the moment).

The second problem was the event loop. Having both a window and a screen open requires processing all window events and then checking for screen events if desired before flipping the buffers. I put in an event loop to accomplish this.

Third, you responded to the events of closing the window and pushing an 'H' by closing the window and the screen but still ran the event loop after those events executed. I placed the End command as the final statement after each of these events trigger.

Last of all I modified your Goto loop to use a Repeat/Forever loop as the Goto loop didn't seem to be selected for any particular purpose.


The program now seems to purform the purpose of animating some text on the window and allow exiting the program by pushing 'H' or closing the window.

Re: Keyboard input

Posted: Tue May 03, 2022 1:29 pm
by pfaber11
Thankyou very much I thought it might be something wrong with my laptop so decided to try it on my sons computer and I was getting the same result so I knew it was my fault . I'm now going to study it and hopefully I'll understand what and how it is done . I made a small program a few years ago with PureBasic but this is like starting over although I do remember some of it . Thanks for helping me out .

Re: Keyboard input

Posted: Tue May 03, 2022 6:58 pm
by Jeff8888
Try using keyboardreleased() instead of keyboardpushed().

Re: Keyboard input

Posted: Wed May 04, 2022 4:42 am
by pfaber11
Thanks for the good advice .

Re: Keyboard input

Posted: Wed Jun 22, 2022 9:15 am
by Olli
Please fire aaronramsdale bot that sends me to a sauerkraut full of advertisements. Thank you.

Code: Select all

Structure main   
    w.I[1]
    h.I[1]
    screenSprite.I
    *video
    angle.F
EndStructure

Procedure Elapsed()
    t = 240000 - ElapsedMilliseconds()
    If t < 0
     End
    EndIf
    ProcedureReturn t
EndProcedure

Define *this.main = AllocateMemory(SizeOf(main) )
With *this
    InitSprite()
    InitKeyboard()
    InitMouse()
    ExamineDesktops()
    \w[0] = DesktopWidth(0)
    \h[0] = DesktopHeight(0)
    OpenScreen(\w[0], \h[0], 32, "", #PB_Screen_SmartSynchronization, DesktopFrequency(0) )
    \screenSprite = CreateSprite(#PB_Any, 320, 200, #PB_Sprite_AlphaBlending)
    ZoomSprite(\screenSprite, \w[0], \h[0] )   
    \angle = 0.0
    Repeat
        t0 = t
        t = Elapsed()
        delta = 16 - (t - t0) 
        If delta < 1
            delta = 1
        EndIf
        If delta > 17
            delta = 17
        EndIf
        Delay(delta)
        t = Elapsed()
        ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
        j + 1
        If StartDrawing(SpriteOutput(\screenSprite) )
            \video = DrawingBuffer()
            For i = 0 To 63999
                x = i % 320
                y = i / 320
                dx = 160 - x
                dy = 100 - y
                c = Sqr(dx*dx+ dy*dy) * (t / 1000)
                df.d = Pow(ATan2(-dy, dx), 2) * (t / 100)
                c + df
                c & 255
                If c < 128 + 64
                    c = 0
                Else
                    If c < 128 + 64 + 16
                        c = RGB(0, 255, 255)
                    Else
                        If c < 128 + 64 + 32
                            c = RGB(0, 0, 255)
                        Else
                            If c < 128 + 64 + 48
                                c = RGB(0, 255, 0)
                            Else
                                c = RGB(255, 0, 0)
                            EndIf
                        EndIf
                    EndIf                 
                EndIf
                PokeL(\video + i << 2, c)
            Next
            StopDrawing()
        EndIf
        ClearScreen(0)
        SpriteQuality(0)
        DisplaySprite(\screenSprite, 0, 0)       
        If StartDrawing(ScreenOutput() )
            DrawText(0, 0, Hex(t) )
            StopDrawing()
        EndIf
        FlipBuffers()
    ForEver    
EndWith