How can i use get keyboard input using thread ?

Advanced game related topics
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

How can i use get keyboard input using thread ?

Post by skinkairewalker »

hi everyone !

i am trying Use Thread to get keyboard input to move image ...

using this code :

Code: Select all


If InitKeyboard() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Can't initialize the sprite system.", 0)
  End
EndIf

Global x.i
Global y.i

  x = 100
  y = 100

MessageRequester("Information", "This will test the fast keyboard access..."+#LF$+"Press 'ESC' to quit!", 0)

Procedure th(val.i)
  
  Debug "Thread created"
  
  Repeat
    
    If KeyboardPushed(#PB_Key_Left)
      x-1
    EndIf
    
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Right)
      x+1
    EndIf
  
    If KeyboardPushed(#PB_Key_Up)
      y-1
    EndIf
  
    If KeyboardPushed(#PB_Key_Down)
      y+1
    EndIf
    
  Until KeyboardPushed(#PB_Key_Escape)
  
EndProcedure  


If OpenScreen(1366, 768, 32, "Keyboard")
  
  LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")
  
  CreateThread(@th,0) 
  
  Repeat
  
    FlipBuffers()
    
    ClearScreen(RGB(0,0,0))
  
    ExamineKeyboard()
    
        For OffsetY=0 To 600 Step 70
      For OffsetX=0 To 800 Step 200
        DisplaySprite(0, OffsetX+x, y+OffsetY)
      Next
      
      For OffsetX=0 To 800 Step 200
        DisplaySprite(0, OffsetX+x+90, y+OffsetY+35)
      Next
    Next
    
  Until KeyboardPushed(#PB_Key_Escape)

Else
  MessageRequester("Error", "Impossible to open a 800*600 32 bit screen",0)
EndIf

End   

but dont works ... how can i do this concept works ?
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: How can i use get keyboard input using thread ?

Post by Marc56us »

skinkairewalker wrote:hi everyone !

i am trying Use Thread to get keyboard input to move image ...
...
but dont works ... how can i do this concept works ?
Hi skinkairewalker ,

Threads are primarily used to run part of the program in the background (without user interaction). So wanting to use them to manage the keyboard is not a good idea :)
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: How can i use get keyboard input using thread ?

Post by skinkairewalker »

Thanks by you Answer Marc56us , on this code :

Code: Select all

If InitKeyboard() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Can't initialize the sprite system.", 0)
  End
EndIf

Global LastKeyInput.i
Global created.i=0
Global  x = 100
Global  y = 100


Procedure thr(val.i)
  
  Repeat
    Debug LastKeyInput
  ForEver
  
EndProcedure

Procedure.i GetKeyPushed()
      ExamineKeyboard()
  If KeyboardPushed(#PB_Key_1)
    ProcedureReturn #PB_Key_1
  EndIf  
  If KeyboardPushed(#PB_Key_2)
    ProcedureReturn #PB_Key_2
  EndIf  
  If KeyboardPushed(#PB_Key_3)
    ProcedureReturn #PB_Key_3
  EndIf  
  If KeyboardPushed(#PB_Key_4)
    ProcedureReturn #PB_Key_4
  EndIf  
  If KeyboardPushed(#PB_Key_5)
    ProcedureReturn #PB_Key_5  
  EndIf  
  If KeyboardPushed(#PB_Key_6)
    ProcedureReturn #PB_Key_6
  EndIf  
  If KeyboardPushed(#PB_Key_7)
    ProcedureReturn #PB_Key_7
  EndIf  
  If KeyboardPushed(#PB_Key_8)
    ProcedureReturn #PB_Key_8
  EndIf  
  If KeyboardPushed(#PB_Key_9)
    ProcedureReturn #PB_Key_9
  EndIf  
  If KeyboardPushed(#PB_Key_0)
    ProcedureReturn #PB_Key_0  
  EndIf  
  If KeyboardPushed(#PB_Key_A)
    ProcedureReturn #PB_Key_A  
  EndIf  
  If KeyboardPushed(#PB_Key_B)
    ProcedureReturn #PB_Key_B 
  EndIf  
  If KeyboardPushed(#PB_Key_C)
    ProcedureReturn #PB_Key_C  
  EndIf  
  If KeyboardPushed(#PB_Key_D)
    ProcedureReturn #PB_Key_D  
  EndIf  
  If KeyboardPushed(#PB_Key_E)
    ProcedureReturn #PB_Key_E  
  EndIf  
  If KeyboardPushed(#PB_Key_F)
    ProcedureReturn #PB_Key_F  
  EndIf  
  If KeyboardPushed(#PB_Key_G)
    ProcedureReturn #PB_Key_G  
  EndIf  
  If KeyboardPushed(#PB_Key_H)
    ProcedureReturn #PB_Key_H  
  EndIf  
  If KeyboardPushed(#PB_Key_I)
    ProcedureReturn #PB_Key_I  
  EndIf  
  If KeyboardPushed(#PB_Key_J)
    ProcedureReturn #PB_Key_J  
  EndIf  
  If KeyboardPushed(#PB_Key_K)
    ProcedureReturn #PB_Key_K
  EndIf  
  If KeyboardPushed(#PB_Key_L)
    ProcedureReturn #PB_Key_L  
  EndIf  
  If KeyboardPushed(#PB_Key_M)
    ProcedureReturn #PB_Key_M  
  EndIf  
  If KeyboardPushed(#PB_Key_N)
    ProcedureReturn #PB_Key_N  
  EndIf  
  If KeyboardPushed(#PB_Key_O)
    ProcedureReturn #PB_Key_O  
  EndIf  
  If KeyboardPushed(#PB_Key_P)
    ProcedureReturn #PB_Key_P  
  EndIf  
  If KeyboardPushed(#PB_Key_Q)
    ProcedureReturn #PB_Key_Q  
  EndIf  
  If KeyboardPushed(#PB_Key_R)
    ProcedureReturn #PB_Key_R  
  EndIf  
  If KeyboardPushed(#PB_Key_S)
    ProcedureReturn #PB_Key_S  
  EndIf  
  If KeyboardPushed(#PB_Key_T)
    ProcedureReturn #PB_Key_T  
  EndIf  
  If KeyboardPushed(#PB_Key_U)
    ProcedureReturn #PB_Key_U  
  EndIf  
  If KeyboardPushed(#PB_Key_V)
    ProcedureReturn #PB_Key_V  
  EndIf  
  If KeyboardPushed(#PB_Key_W)
    ProcedureReturn #PB_Key_W  
  EndIf  
  If KeyboardPushed(#PB_Key_X)
    ProcedureReturn #PB_Key_X  
  EndIf  
  If KeyboardPushed(#PB_Key_Y)
    ProcedureReturn #PB_Key_Y  
  EndIf  
  If KeyboardPushed(#PB_Key_Z)
    ProcedureReturn #PB_Key_Z
  EndIf  
  If KeyboardPushed(#PB_Key_Escape)
    ProcedureReturn #PB_Key_Escape
  EndIf  
  If KeyboardPushed(#PB_Key_Minus)
    ProcedureReturn #PB_Key_Minus
  EndIf  
  If KeyboardPushed(#PB_Key_Equals)
    ProcedureReturn #PB_Key_Equals
  EndIf
  If KeyboardPushed(#PB_Key_Back)
    ProcedureReturn #PB_Key_Back
  EndIf  
  If KeyboardPushed(#PB_Key_Tab)
    ProcedureReturn #PB_Key_Tab
  EndIf  
  If KeyboardPushed(#PB_Key_LeftBracket)
    ProcedureReturn #PB_Key_LeftBracket
  EndIf  
  If KeyboardPushed(#PB_Key_RightBracket)
    ProcedureReturn #PB_Key_RightBracket
  EndIf  
  If KeyboardPushed(#PB_Key_Return)
    ProcedureReturn #PB_Key_Return 
  EndIf  
  If KeyboardPushed(#PB_Key_LeftControl)
    ProcedureReturn #PB_Key_LeftControl 
  EndIf  
  If KeyboardPushed(#PB_Key_SemiColon)
    ProcedureReturn #PB_Key_SemiColon 
  EndIf  
  If KeyboardPushed(#PB_Key_Apostrophe)
    ProcedureReturn #PB_Key_Apostrophe 
  EndIf  
  If KeyboardPushed(#PB_Key_Grave)
    ProcedureReturn #PB_Key_Grave 
  EndIf  
  If KeyboardPushed(#PB_Key_LeftShift)
    ProcedureReturn #PB_Key_LeftShift   
  EndIf  
  If KeyboardPushed(#PB_Key_BackSlash)
    ProcedureReturn #PB_Key_BackSlash 
  EndIf  
  If KeyboardPushed(#PB_Key_Comma)
    ProcedureReturn #PB_Key_Comma 
  EndIf  
  If KeyboardPushed(#PB_Key_Period)
    ProcedureReturn #PB_Key_Period   
  EndIf  
  If KeyboardPushed(#PB_Key_Slash)
    ProcedureReturn #PB_Key_Slash 
  EndIf  
  If KeyboardPushed(#PB_Key_RightShift)
    ProcedureReturn #PB_Key_RightShift 
  EndIf  
  If KeyboardPushed(#PB_Key_Multiply)
    ProcedureReturn #PB_Key_Multiply   
  EndIf  
  If KeyboardPushed(#PB_Key_LeftAlt)
    ProcedureReturn #PB_Key_LeftAlt
  EndIf  
  If KeyboardPushed(#PB_Key_Space)
    ProcedureReturn #PB_Key_Space 
  EndIf  
  If KeyboardPushed(#PB_Key_Capital)
    ProcedureReturn #PB_Key_Capital 
  EndIf  
  If KeyboardPushed(#PB_Key_F1)
    ProcedureReturn #PB_Key_F1 
  EndIf  
  If KeyboardPushed(#PB_Key_F2)
    ProcedureReturn #PB_Key_F2 
  EndIf  
  If KeyboardPushed(#PB_Key_F3)
    ProcedureReturn #PB_Key_F3 
  EndIf  
  If KeyboardPushed(#PB_Key_F4)
    ProcedureReturn #PB_Key_F4 
  EndIf  
  If KeyboardPushed(#PB_Key_F5)
    ProcedureReturn #PB_Key_F5 
  EndIf  
  If KeyboardPushed(#PB_Key_F6)
    ProcedureReturn #PB_Key_F6 
  EndIf  
  If KeyboardPushed(#PB_Key_F7)
    ProcedureReturn #PB_Key_F7 
  EndIf  
  If KeyboardPushed(#PB_Key_F8)
    ProcedureReturn #PB_Key_F8 
  EndIf  
  If KeyboardPushed(#PB_Key_F9)
    ProcedureReturn #PB_Key_F9 
  EndIf  
  If KeyboardPushed(#PB_Key_F10)
    ProcedureReturn #PB_Key_F10 
  EndIf  
  If KeyboardPushed(#PB_Key_F11)
    ProcedureReturn #PB_Key_F11 
  EndIf  
  If KeyboardPushed(#PB_Key_F12)
    ProcedureReturn #PB_Key_F12 
  EndIf  
  If KeyboardPushed(#PB_Key_NumLock)
    ProcedureReturn #PB_Key_NumLock 
  EndIf  
  If KeyboardPushed(#PB_Key_Scroll)
    ProcedureReturn #PB_Key_Scroll 
  EndIf  
  If KeyboardPushed(#PB_Key_Pad0)
    ProcedureReturn #PB_Key_Pad0 
  EndIf  
  If KeyboardPushed(#PB_Key_Pad1)
    ProcedureReturn #PB_Key_Pad1 
  EndIf  
  If KeyboardPushed(#PB_Key_Pad2)
    ProcedureReturn #PB_Key_Pad2 
  EndIf  
  If KeyboardPushed(#PB_Key_Pad3)
    ProcedureReturn #PB_Key_Pad3  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad4)
    ProcedureReturn #PB_Key_Pad4  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad5)
    ProcedureReturn #PB_Key_Pad5  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad6)
    ProcedureReturn #PB_Key_Pad6  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad7)
    ProcedureReturn #PB_Key_Pad7  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad8)
    ProcedureReturn #PB_Key_Pad8  
  EndIf  
  If KeyboardPushed(#PB_Key_Pad9)
    ProcedureReturn #PB_Key_Pad9  
  EndIf  
  If KeyboardPushed(#PB_Key_Add)
    ProcedureReturn #PB_Key_Add  
  EndIf  
  If KeyboardPushed(#PB_Key_Subtract)
    ProcedureReturn #PB_Key_Subtract  
  EndIf  
  If KeyboardPushed(#PB_Key_Decimal)
    ProcedureReturn #PB_Key_Decimal  
  EndIf  
  If KeyboardPushed(#PB_Key_PadEnter)
    ProcedureReturn #PB_Key_PadEnter  
  EndIf  
  If KeyboardPushed(#PB_Key_RightControl)
    ProcedureReturn #PB_Key_RightControl  
  EndIf  
  If KeyboardPushed(#PB_Key_PadComma)
    ProcedureReturn #PB_Key_PadComma  
  EndIf  
  If KeyboardPushed(#PB_Key_Divide)
    ProcedureReturn #PB_Key_Divide  
  EndIf  
  If KeyboardPushed(#PB_Key_RightAlt)
    ProcedureReturn #PB_Key_RightAlt  
  EndIf  
  If KeyboardPushed(#PB_Key_Pause)
    ProcedureReturn #PB_Key_Pause  
  EndIf  
  If KeyboardPushed(#PB_Key_Home)
    ProcedureReturn #PB_Key_Home  
  EndIf  
  If KeyboardPushed(#PB_Key_Up)
    ProcedureReturn #PB_Key_Up  
  EndIf  
  If KeyboardPushed(#PB_Key_Down)
    ProcedureReturn #PB_Key_Down  
  EndIf  
  If KeyboardPushed(#PB_Key_Left)
    ProcedureReturn #PB_Key_Left  
  EndIf  
  If KeyboardPushed(#PB_Key_Right)
    ProcedureReturn #PB_Key_Right    
  EndIf  
  If KeyboardPushed(#PB_Key_End)
    ProcedureReturn #PB_Key_End  
  EndIf  
  If KeyboardPushed(#PB_Key_PageUp)
    ProcedureReturn #PB_Key_PageUp  
  EndIf  
  If KeyboardPushed(#PB_Key_PageDown)
    ProcedureReturn #PB_Key_PageDown  
  EndIf  
  If KeyboardPushed(#PB_Key_Insert)
    ProcedureReturn #PB_Key_Insert  
  EndIf  
  If KeyboardPushed(#PB_Key_Delete)
    ProcedureReturn #PB_Key_Delete    
  EndIf  
  ;If KeyboardPushed(PB_Key_All)
  ;  ProcedureReturn PB_Key_All      
  ;EndIf
  
EndProcedure  

Procedure.i GetKeyPushedSingle()
      ExamineKeyboard()
  If KeyboardPushed(#PB_Key_1)
    ProcedureReturn #PB_Key_1
  ElseIf KeyboardPushed(#PB_Key_2)
    ProcedureReturn #PB_Key_2
  ElseIf KeyboardPushed(#PB_Key_3)
    ProcedureReturn #PB_Key_3
  ElseIf KeyboardPushed(#PB_Key_4)
    ProcedureReturn #PB_Key_4
  ElseIf KeyboardPushed(#PB_Key_5)
    ProcedureReturn #PB_Key_5  
  ElseIf KeyboardPushed(#PB_Key_6)
    ProcedureReturn #PB_Key_6
  ElseIf KeyboardPushed(#PB_Key_7)
    ProcedureReturn #PB_Key_7
  ElseIf KeyboardPushed(#PB_Key_8)
    ProcedureReturn #PB_Key_8
  ElseIf KeyboardPushed(#PB_Key_9)
    ProcedureReturn #PB_Key_9
  ElseIf KeyboardPushed(#PB_Key_0)
    ProcedureReturn #PB_Key_0  
  ElseIf KeyboardPushed(#PB_Key_A)
    ProcedureReturn #PB_Key_A  
  ElseIf KeyboardPushed(#PB_Key_B)
    ProcedureReturn #PB_Key_B 
  ElseIf KeyboardPushed(#PB_Key_C)
    ProcedureReturn #PB_Key_C  
  ElseIf KeyboardPushed(#PB_Key_D)
    ProcedureReturn #PB_Key_D  
  ElseIf KeyboardPushed(#PB_Key_E)
    ProcedureReturn #PB_Key_E  
  ElseIf KeyboardPushed(#PB_Key_F)
    ProcedureReturn #PB_Key_F  
  ElseIf KeyboardPushed(#PB_Key_G)
    ProcedureReturn #PB_Key_G  
  ElseIf KeyboardPushed(#PB_Key_H)
    ProcedureReturn #PB_Key_H  
  ElseIf KeyboardPushed(#PB_Key_I)
    ProcedureReturn #PB_Key_I  
  ElseIf KeyboardPushed(#PB_Key_J)
    ProcedureReturn #PB_Key_J  
  ElseIf KeyboardPushed(#PB_Key_K)
    ProcedureReturn #PB_Key_K
  ElseIf KeyboardPushed(#PB_Key_L)
    ProcedureReturn #PB_Key_L  
  ElseIf KeyboardPushed(#PB_Key_M)
    ProcedureReturn #PB_Key_M  
  ElseIf KeyboardPushed(#PB_Key_N)
    ProcedureReturn #PB_Key_N  
  ElseIf KeyboardPushed(#PB_Key_O)
    ProcedureReturn #PB_Key_O  
  ElseIf KeyboardPushed(#PB_Key_P)
    ProcedureReturn #PB_Key_P  
  ElseIf KeyboardPushed(#PB_Key_Q)
    ProcedureReturn #PB_Key_Q  
  ElseIf KeyboardPushed(#PB_Key_R)
    ProcedureReturn #PB_Key_R  
  ElseIf KeyboardPushed(#PB_Key_S)
    ProcedureReturn #PB_Key_S  
  ElseIf KeyboardPushed(#PB_Key_T)
    ProcedureReturn #PB_Key_T  
  ElseIf KeyboardPushed(#PB_Key_U)
    ProcedureReturn #PB_Key_U  
  ElseIf KeyboardPushed(#PB_Key_V)
    ProcedureReturn #PB_Key_V  
  ElseIf KeyboardPushed(#PB_Key_W)
    ProcedureReturn #PB_Key_W  
  ElseIf KeyboardPushed(#PB_Key_X)
    ProcedureReturn #PB_Key_X  
  ElseIf KeyboardPushed(#PB_Key_Y)
    ProcedureReturn #PB_Key_Y  
  ElseIf KeyboardPushed(#PB_Key_Z)
    ProcedureReturn #PB_Key_Z
  ElseIf KeyboardPushed(#PB_Key_Escape)
    ProcedureReturn #PB_Key_Escape
  ElseIf KeyboardPushed(#PB_Key_Minus)
    ProcedureReturn #PB_Key_Minus
  ElseIf KeyboardPushed(#PB_Key_Equals)
    ProcedureReturn #PB_Key_Equals
  ElseIf KeyboardPushed(#PB_Key_Back)
    ProcedureReturn #PB_Key_Back
  ElseIf KeyboardPushed(#PB_Key_Tab)
    ProcedureReturn #PB_Key_Tab
  ElseIf KeyboardPushed(#PB_Key_LeftBracket)
    ProcedureReturn #PB_Key_LeftBracket
  ElseIf KeyboardPushed(#PB_Key_RightBracket)
    ProcedureReturn #PB_Key_RightBracket
  ElseIf KeyboardPushed(#PB_Key_Return)
    ProcedureReturn #PB_Key_Return 
  ElseIf KeyboardPushed(#PB_Key_LeftControl)
    ProcedureReturn #PB_Key_LeftControl 
  ElseIf KeyboardPushed(#PB_Key_SemiColon)
    ProcedureReturn #PB_Key_SemiColon 
  ElseIf KeyboardPushed(#PB_Key_Apostrophe)
    ProcedureReturn #PB_Key_Apostrophe 
  ElseIf KeyboardPushed(#PB_Key_Grave)
    ProcedureReturn #PB_Key_Grave 
  ElseIf KeyboardPushed(#PB_Key_LeftShift)
    ProcedureReturn #PB_Key_LeftShift   
  ElseIf KeyboardPushed(#PB_Key_BackSlash)
    ProcedureReturn #PB_Key_BackSlash 
  ElseIf KeyboardPushed(#PB_Key_Comma)
    ProcedureReturn #PB_Key_Comma 
  ElseIf KeyboardPushed(#PB_Key_Period)
    ProcedureReturn #PB_Key_Period   
  ElseIf KeyboardPushed(#PB_Key_Slash)
    ProcedureReturn #PB_Key_Slash 
  ElseIf KeyboardPushed(#PB_Key_RightShift)
    ProcedureReturn #PB_Key_RightShift 
  ElseIf KeyboardPushed(#PB_Key_Multiply)
    ProcedureReturn #PB_Key_Multiply   
  ElseIf KeyboardPushed(#PB_Key_LeftAlt)
    ProcedureReturn #PB_Key_LeftAlt
  ElseIf KeyboardPushed(#PB_Key_Space)
    ProcedureReturn #PB_Key_Space 
  ElseIf KeyboardPushed(#PB_Key_Capital)
    ProcedureReturn #PB_Key_Capital 
  ElseIf KeyboardPushed(#PB_Key_F1)
    ProcedureReturn #PB_Key_F1 
  ElseIf KeyboardPushed(#PB_Key_F2)
    ProcedureReturn #PB_Key_F2 
  ElseIf KeyboardPushed(#PB_Key_F3)
    ProcedureReturn #PB_Key_F3 
  ElseIf KeyboardPushed(#PB_Key_F4)
    ProcedureReturn #PB_Key_F4 
  ElseIf KeyboardPushed(#PB_Key_F5)
    ProcedureReturn #PB_Key_F5 
  ElseIf KeyboardPushed(#PB_Key_F6)
    ProcedureReturn #PB_Key_F6 
  ElseIf KeyboardPushed(#PB_Key_F7)
    ProcedureReturn #PB_Key_F7 
  ElseIf KeyboardPushed(#PB_Key_F8)
    ProcedureReturn #PB_Key_F8 
  ElseIf KeyboardPushed(#PB_Key_F9)
    ProcedureReturn #PB_Key_F9 
  ElseIf KeyboardPushed(#PB_Key_F10)
    ProcedureReturn #PB_Key_F10 
  ElseIf KeyboardPushed(#PB_Key_F11)
    ProcedureReturn #PB_Key_F11 
  ElseIf KeyboardPushed(#PB_Key_F12)
    ProcedureReturn #PB_Key_F12 
  ElseIf KeyboardPushed(#PB_Key_NumLock)
    ProcedureReturn #PB_Key_NumLock 
  ElseIf KeyboardPushed(#PB_Key_Scroll)
    ProcedureReturn #PB_Key_Scroll 
  ElseIf KeyboardPushed(#PB_Key_Pad0)
    ProcedureReturn #PB_Key_Pad0 
  ElseIf KeyboardPushed(#PB_Key_Pad1)
    ProcedureReturn #PB_Key_Pad1 
  ElseIf KeyboardPushed(#PB_Key_Pad2)
    ProcedureReturn #PB_Key_Pad2 
  ElseIf KeyboardPushed(#PB_Key_Pad3)
    ProcedureReturn #PB_Key_Pad3  
  ElseIf KeyboardPushed(#PB_Key_Pad4)
    ProcedureReturn #PB_Key_Pad4  
  ElseIf KeyboardPushed(#PB_Key_Pad5)
    ProcedureReturn #PB_Key_Pad5  
  ElseIf KeyboardPushed(#PB_Key_Pad6)
    ProcedureReturn #PB_Key_Pad6  
  ElseIf KeyboardPushed(#PB_Key_Pad7)
    ProcedureReturn #PB_Key_Pad7  
  ElseIf KeyboardPushed(#PB_Key_Pad8)
    ProcedureReturn #PB_Key_Pad8  
  ElseIf KeyboardPushed(#PB_Key_Pad9)
    ProcedureReturn #PB_Key_Pad9  
  ElseIf KeyboardPushed(#PB_Key_Add)
    ProcedureReturn #PB_Key_Add  
  ElseIf KeyboardPushed(#PB_Key_Subtract)
    ProcedureReturn #PB_Key_Subtract  
  ElseIf KeyboardPushed(#PB_Key_Decimal)
    ProcedureReturn #PB_Key_Decimal  
  ElseIf KeyboardPushed(#PB_Key_PadEnter)
    ProcedureReturn #PB_Key_PadEnter  
  ElseIf KeyboardPushed(#PB_Key_RightControl)
    ProcedureReturn #PB_Key_RightControl  
  ElseIf KeyboardPushed(#PB_Key_PadComma)
    ProcedureReturn #PB_Key_PadComma  
  ElseIf KeyboardPushed(#PB_Key_Divide)
    ProcedureReturn #PB_Key_Divide  
  ElseIf KeyboardPushed(#PB_Key_RightAlt)
    ProcedureReturn #PB_Key_RightAlt  
  ElseIf KeyboardPushed(#PB_Key_Pause)
    ProcedureReturn #PB_Key_Pause  
  ElseIf KeyboardPushed(#PB_Key_Home)
    ProcedureReturn #PB_Key_Home  
  ElseIf KeyboardPushed(#PB_Key_Up)
    ProcedureReturn #PB_Key_Up  
  ElseIf KeyboardPushed(#PB_Key_Down)
    ProcedureReturn #PB_Key_Down  
  ElseIf KeyboardPushed(#PB_Key_Left)
    ProcedureReturn #PB_Key_Left  
  ElseIf KeyboardPushed(#PB_Key_Right)
    ProcedureReturn #PB_Key_Right    
  ElseIf KeyboardPushed(#PB_Key_End)
    ProcedureReturn #PB_Key_End  
  ElseIf KeyboardPushed(#PB_Key_PageUp)
    ProcedureReturn #PB_Key_PageUp  
  ElseIf KeyboardPushed(#PB_Key_PageDown)
    ProcedureReturn #PB_Key_PageDown  
  ElseIf KeyboardPushed(#PB_Key_Insert)
    ProcedureReturn #PB_Key_Insert  
  ElseIf KeyboardPushed(#PB_Key_Delete)
    ProcedureReturn #PB_Key_Delete    
  ElseIf KeyboardPushed(PB_Key_All)
    ProcedureReturn PB_Key_All    
  EndIf
  
EndProcedure  

Procedure showSprite()
  
  
EndProcedure  

    ;If (GetKeyPushed() = #PB_Key_Left)
    ;  x-1
    ;EndIf
  
   ; If (GetKeyPushed() = #PB_Key_Right)
   ;   x+1
   ; EndIf
  
   ; If (GetKeyPushed() = #PB_Key_Up)
   ;   y-1
  ;  EndIf
  
   ; If (GetKeyPushed() = #PB_Key_Down)
   ;   y+1
    ;EndIf
    

MessageRequester("Information", "This will test the fast keyboard access..."+#LF$+"Press 'ESC' to quit!", 0)


If OpenWindow(0,0,0,800,600,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(0),0,0,800,600,#True,0,#PB_Screen_WaitSynchronization )

  
  LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")
  
  Repeat
    
    event = WindowEvent()
    FlipBuffers()
    
    ClearScreen(RGB(0,0,0))
 
    
    If (created = 0)
      CreateThread(@thr(),0)
      created = 1
    EndIf  
    
    LastKeyInput = GetKeyPushedSingle()
    
    If (GetKeyPushed() = #PB_Key_Left)
      x-1
    ElseIf (GetKeyPushed() = #PB_Key_Right)
      x+1
    ElseIf (GetKeyPushedSingle() = #PB_Key_Up)
      y-1
    ElseIf (GetKeyPushedSingle() = #PB_Key_Down)
      y+1
    EndIf

    
    For OffsetY=0 To 600 Step 70
      For OffsetX=0 To 800 Step 200
        DisplaySprite(0, OffsetX+x, y+OffsetY)
      Next
      
      For OffsetX=0 To 800 Step 200
        DisplaySprite(0, OffsetX+x+90, y+OffsetY+35)
      Next
    Next
    
  Until KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow

Else
  MessageRequester("Error", "Impossible to open a 800*600 32 bit screen",0)
EndIf

End  
i am trying get pressed key button ... but have something wrong .

When i set keys " up " , " right " , " left " and " down " , if i press "up" and try press " right " or " left ", the sprite just follow " up " direction , same for " down " .
but when i press " right " or " left " , and try press " up " or " down " , the sprites stop following "right" or "left" and then follow the "up" and "down" .


-------------------------------------------------------------------------------------------------------------------------------------------

now when I set the "W", "A", "S" and "D" keys to control the direction of the sprites, when I press "W" or "S" to move them up and down if I If you press "A" or "D" it stops moving up and down, and then you go right and left, then when I press right and left, I can not make them stop following right and left to be able to follow up and down ...


I hope you understood the problem ... :)
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: How can i use get keyboard input using thread ?

Post by zefiro_flashparty »

you have to take into account several things
When one creates a thread, it runs at a speed independent of the program, so if the tread passes a thousand times in a second.
when you press a key it will be pressed a thousand times ..
I tried to see your example but I was completely out of range towards the side.
with which perhaps it is convenient to add some kind of delay
Or take the keys after plotting which, never go faster than what you see, or slower, only at the same speed.
another thing when you use shared variables within the thread
uses shared () and is updated in both

when someone uses threads, usually some type of flag is used,
because if you want to modify x or y at the same time, you can crash the program. and hang up the pc.
usually with shared I have not had problems, but in a more complex process, it can cause problems,



Procedure th(val.i)

Debug "Thread created"
Shared x
Shared y

repeat
;blablabla

Delay (30);<---- to slow speed
ForEver
EndProcedure

if you look at your code with a debug "left" in the thread
you will see that they are preached many times
and the sprites are going away from the screen
I never look at the FPS, but maybe I should synchronize it with that.
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: How can i use get keyboard input using thread ?

Post by zefiro_flashparty »

another thing that occurs to me is to create an Xtemporal, in the thread,
and that being different from 0 in the main program
move x and - + 1 and then the xtemporal = 0, so that it only takes one scroll once

If KeyboardPushed(#PB_Key_Left)
xt=-1
EndIf


and in the other side.
if xt=-1
x-1
xt=0
endif

:v
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
Post Reply