Menu

KeyEvent

Help
Anonymous
2014-09-01
2019-06-06
  • Anonymous

    Anonymous - 2014-09-01

    Is there a possibility to get information about pressed keys in a graphic window, without stop the programm until a key is pressed?

    I am using the ms windows version of X11 basic.

     
  • Markus Hoffmann

    Markus Hoffmann - 2014-12-25

    Wait! Have you tried the EVENT?() function? It can tell you at least if an event is pending without waiting for it.

     
  • Markus Hoffmann

    Markus Hoffmann - 2015-05-08

    Ok, I see what you mean. It is currently not working under WINDOWS. I am working on that. The next release 1.23-22 should have improved EVENT command and EVENT?() function, which together should do the job.

     
  • Anonymous

    Anonymous - 2018-08-06

    Hi,
    so is it working now? I cannot get this work on my android device. It is crucial function to me to create program which is doing something and I can interrupt it with a keypress. do you have example code please?

     
    • Yet Another Troll

      Here is something to try. It seems to work on Win32, even though I was mainly investigating some Android quirks having to do with rotating the Android device and getting the program to detect the changed screen parameters and redraw as well as quit on a touch. It does detect and report keypresses.

      #!/usr/bin/xbasic
      
      Program Sined
      
       ' Initialize global variables
       Clr ScaleLeft, ScaleTop, ScaleRight, ScaleBottom
       Clr ScaleHM, ScaleHB, ScaleVM, ScaleVB
      
       '@FlushEvents
       @Main
       '@FlushEvents
      End
      
      Procedure Main()
       Local PrevLeft%, PrevTop%, PrevWidth%, PrevHeight%
       Local CurrLeft%, CurrTop%, CurrWidth%, CurrHeight%
       Local QuittingTime%, RedrawNeeded%
       Local EventMask%, EventType%
       Local KEvents%, MEvents%, WEvents%
      
       ' Cls
       Screen 28 ! 1280x1024x24, no-op on Android?
       Get_ScreenSize PrevLeft%, PrevTop%, PrevWidth%, PrevHeight%
       Print PrevLeft%, PrevTop%, PrevWidth%, PrevHeight%
      
       ' All no-op on Android?
       OpenW 1
       MoveW 1, 10, 20
       SizeW 1, 800, 480
       TitleW 1, "Sined, Sealed, Delivered"
       ShowPage
       Pause 0.1
      
       ' Initialize the event loop
       Clr KEvents%, MEvents%, WEvents%
       Clr EventMask%, EventType%
       QuittingTime% = False
       RedrawNeeded% = True
      
       ' Our very first event pump in X11 BASIC 
       While (EventType% <> 0) Or Not QuittingTime%
      
        ' Looks like we need to poll for resizes due to device rotations 
        ' or the on screen keyboard being summoned or dismissed
        Get_Geometry 1, CurrLeft%, CurrTop%, CurrWidth%, CurrHeight%
        ' Is the left, top coordinate ever not (0, 0) ?
        If (CurrWidth% <> PrevWidth%) Or (CurrHeight% <> PrevHeight%) Then
         Print "Geometry changed"
         PrevLeft% = CurrLeft%
         PrevTop% = CurrTop%
         PrevWidth% = CurrWidth%
         PrevHeight% = CurrHeight%
         RedrawNeeded% = True
         ' Device rotations also appear to cause spurious kb events?
         ' @FlushEvents
         ' Cls
        EndIf
      
        If RedrawNeeded% Then
         Print "Redrawing"
         @Redraw()
         RedrawNeeded% = False
        EndIf
      
        ' Peek at any/all pending events, keyboard/mouse/window
        EventMask% = 1 Or 2 Or 4 Or 8 Or 0x40 Or 0x40000 Or 0x200000
        Print Hex$(EventMask%); " ";
        EventMask% = Event?(EventMask%)
        Print Hex$(EventMask%); " ";
      
        If EventMask% Then
         ' Fetch a pending event
         Event EventType% ! Lots of fun optional out parameters can go here
         Print EventType%; " ";
        Else
         ' No events are waiting
         EventType% = 0
        EndIf
      
        Select EventType%
         Case 10, 13
          ' Window moved or resized
          Print "Window"
          Inc WEvents%
          RedrawNeeded% = True
         Case 2, 3
          ' Keyboard key down/up
          Print "Keybd"
          Inc KEvents%
          ' Beware of spurious kb events on Android
          ' QuittingTime% = True
         Case 4, 5, 6
          ' Mouse button down/up, move
          Print "Mouse"
          Inc MEvents%
          QuittingTime% = True
         Default
          ' No news is good news
          Print "Nada"
        EndSelect
      
        If (EventType% = 0) And Not QuittingTime% Then
         Pause 1
        EndIf 
      
       Wend
      
       Print KEvents%; " key, ";  MEvents%; " mouse, "; WEvents%; " window"
      
      Return
      
      Procedure Redraw()
       Local Left%, Top%, Width%, Height%
       Local cX, cY, pX, pY, theta
      
       OpenW 1
       Get_Geometry 1, Left%, Top%, Width%, Height%
       Print Left%, Top%, Width% - 1, Height% - 1
      
       ' Sanity check coordinate system
      
       Color Color_RGB(0, 0, 1, 0.7)
       Draw Left%, Top% To (Left% + Width% - 1), (Top% + Height% - 1)
       ShowPage
      
       Color Color_RGB(0, 1, 0, 0.7)
       Draw (Left% + Width% - 100), Top%  To Left%, Top%
       Draw To Left%, (Top% + Height% - 1)
       Draw To (Left% + Width% - 1), (Top% + Height% - 1)
       Draw To (Left% + Width% - 1), Top%
       ShowPage
      
       ' Reject screen's reality and substitute our own
       @SetScale(-10, 10, -2, 2)
      
       ' Set up our axes
       Color Color_RGB(1, 1, 1)
       @ScaleLine(-10, 0, 10, 0)
       @ScaleLine(0, 2, 0, -2)
       For cY = 0 To ScaleTop Step 0.25
        @ScaleLine(-0.1, cY, 0.1, cY)
        @ScaleLine(-0.1, -cY, 0.1, -cY)
       Next cY
       For cY = 0 To ScaleTop
        @ScaleLine(-0.25, cY, 0.25, cY)
        @ScaleLine(-0.25, -cY, 0.25, -cY)
       Next cY
       For cX = 0 To ScaleRight Step (Pi / 4)
        @ScaleLine(cX, 0.025, cX, -0.025)
        @ScaleLine(-cX, 0.025, -cX, -0.025)
       Next cX
       For cX = 0 To ScaleRight Step (Pi / 2)
        @ScaleLine(cX, 0.05, cX, -0.05)
        @ScaleLine(-cX, 0.05, -cX, -0.05)
       Next cX
       For cX = 0 To ScaleRight Step Pi
        @ScaleLine(cX, 0.1, cX, -0.1)
        @ScaleLine(-cX, 0.1, -cX, -0.1)
       Next cX
       ShowPage
      
       ' Plot f(x)
       Color Color_RGB(1, 0, 0)
       Clr pX, pY, cX, cY
       pX = 1e200
       For theta = ScaleLeft To ScaleRight Step (Pi / 100)
        cX = theta
        cY = @f(theta)
        If (ScaleLeft <= pX) And (pX <= ScaleRight) Then
         @ScaleLine(pX, pY, cX, cY)
        EndIf
        pX = cX
        pY = cY
       Next theta
       ShowPage
      Return
      
      DefFn f(x) = (Sin(x) + (Cos(5*x)/2))
      
      Procedure SetScale(NewLeft, NewRight, NewBottom, NewTop)
       Local Left%, Top%, Width%, Height%
      
       Get_Geometry 1, Left%, Top%, Width%, Height%
       Print Left%, Top%, Width% - 1, Height% - 1
      
       ScaleLeft = NewLeft
       ScaleTop = NewTop
       ScaleRight = NewRight
       ScaleBottom = NewBottom
      
       ScaleHM = (Width% - 1) / (NewRight - NewLeft)
       ScaleHB = Left% - ((Width% - 1) / (NewRight - NewLeft)) * NewLeft
       ScaleVM = (Height% - 1) / (NewBottom - NewTop)
       ScaleVB = Top% - ((Height% - 1) / (NewBottom - NewTop)) * NewTop
      
       Print ScaleLeft, ScaleRight, ScaleBottom, ScaleTop
       Print ScaleHM, ScaleHB, ScaleVM, ScaleVB
      Return
      
      Procedure ScaleLine(x0, y0, x1, y1)
       Draw ScaleHM * x0 + ScaleHB, ScaleVM * y0 + ScaleVB \
         To ScaleHM * x1 + ScaleHB, ScaleVM * y1 + ScaleVB
      Return
      
      ' Android appears to allow events to accumulate between runs
      Procedure FlushEvents()
       Local EventMask%, EventType%
       Clr EventMask%, EventType%
       Print "FlushEvents ";
       Repeat
        ' Are any events clogging the queue?
        ' Peek at any/all pending events, keyboard/mouse/window
        EventMask% = 1 Or 2 Or 4 Or 8 Or 0x40 Or 0x40000 Or 0x200000
        EventMask% = Event?(EventMask%)
        If EventMask% Then
         ' Catch and kill pending event
         Event EventType%
        Else
         ' No events are waiting
         EventType% = 0
        EndIf
        Print EventType%; " ";
       Until EventType% = 0
       Print ""
      Return
      
      ' Stolen from http://x11-basic.sourceforge.net/FAQ.html
      Function ChrW$(Unicode%)
       If Unicode% < 0x80 Then
        Return Chr$(Unicode%)
       Else If Unicode% < 0x2000 Then
        Return Chr$(0xc0 Or ((Unicode% Div 64) And 0x1f)) \ 
             + Chr$(0x80 Or (Unicode% And 0x3f))
       Else
        Return Chr$(0xe0 Or ((Unicode% Div (64 * 64)) And 0xf)) \
             + Chr$(0x80 Or ((Unicode% Div 64) And 0x3f))       \
             + Chr$(0x80 Or (Unicode% And 0x3f))
       EndIf
      EndFunction
      
       

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.