Utilisation de GetKeyboardState_()

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Ollivier
Messages : 4190
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Utilisation de GetKeyboardState_()

Message par Ollivier »

Voici un exemple de l'utilisation de GetKeyboardState_(). Bon, c'est un peu à la one again mais bon, au moins ça montre le schmilblick.

Code : Tout sélectionner

Macro PushKey(I)

   UpperState = Keys\Key[20] & 1
         
   If Keys\Key[16] & 128
         
      UpperState ! 1
         
   EndIf

   ;If I > 31
   
      ;If I < 128
      
         AlphaNum = 0
   
         If I => 48
         
            If I <= 57

               AlphaNum = 1               
            
            EndIf
         
         EndIf
         
         NumKey = 0
         
         If I => 96
         
            If I <= 105
            
               NumKey = 1
         
            EndIf
         
         EndIf

         AlphaKey = 0
         
         If I => 65
         
            If I <= 90
            
               AlphaKey = 1
         
            EndIf
         
         EndIf

         If AlphaNum

            If UpperState
               
               GInkey + Chr(I)
               
            Else
                  
               GInkey + Mid(CharMap1, I - 47, 1)
               
            EndIf

         Else

            If NumKey

               GInkey + Chr(I - 48)            

            Else

               If AlphaKey

                  If UpperState
      
                     GInkey + Chr(I)
         
                  Else
    
                     GInkey + LCase(Chr(I) )
                     
                  EndIf

               Else
                  
                  If I = 106: GInkey + "*": EndIf
                  If I = 107: GInkey + "+": EndIf
                  If I = 109: GInkey + "-": EndIf
                  If I = 110: GInkey + ".": EndIf
                  If I = 111: GInkey + "/": EndIf
                  
                  If UpperState
                  
                     If I = 186: GInkey + "£": EndIf
                     If I = 187: GInkey + "+": EndIf
                     If I = 188: GInkey + "?": EndIf
                     If I = 190: GInkey + ".": EndIf
                     If I = 191: GInkey + "/": EndIf
                     If I = 192: GInkey + "%": EndIf
                     If I = 219: GInkey + "°": EndIf
                     If I = 220: GInkey + "µ": EndIf
                     If I = 221: GInkey + "¨": EndIf
                     If I = 223: GInkey + "§": EndIf
                  
                  Else

                     If I = 186: GInkey + "$": EndIf
                     If I = 187: GInkey + "=": EndIf
                     If I = 188: GInkey + ",": EndIf
                     If I = 190: GInkey + ";": EndIf
                     If I = 191: GInkey + ":": EndIf
                     If I = 192: GInkey + "ù": EndIf
                     If I = 219: GInkey + ")": EndIf
                     If I = 220: GInkey + "*": EndIf
                     If I = 221: GInkey + "^": EndIf
                     If I = 223: GInkey + "!": EndIf
                     
                  
                  EndIf
            
               EndIf
            
            EndIf
         
         EndIf

      ;EndIf
      
   ;EndIf
   
EndMacro

Macro GetKeyboard(GetTime)

         If GetKeyboardState_(@Keys)
         
            For I = 0 To 255
            
               If Keys\Key[I] & 128
               
                  If Keys\StartTime[I] = 0
                  
                     Keys\StartTime[I] = GetTime
                     
                     Keys\Pushed[I] + 1
                     
                     PushKey(I)
                                       
                  Else
                  
                     If GetTime - Keys\StartTime[I] > 500
                     
                        Keys\Pushed[I] + 1
                        
                        PushKey(I)
                     
                     EndIf               
                  
                  EndIf
               
               Else
               
                  Keys\StartTime[I] = 0
                  
                  Keys\Pushed[I] = 0
               
               EndIf
            
            Next I

         EndIf

EndMacro

Macro InitKeyb()

   Structure FFmap
   
      Key.C[256]
      StartTime.I[256]
      Pushed.I[256]
      
   EndStructure

   Global Keys.FFmap
   Global GInkey.S
   Global UpperState.I
   Global CharMap1.S = "à&é" + Chr(34) + "'(-è_ç"
   Global AlphaNum.I
   Global NumKey.I
   Global AlphaKey.I
   Global SyntaxKey.I
   
EndMacro

   InitKeyb()
   
   Define WinX.I
   Define Exit.I
   Define OldGInkey.S

   WinX = OpenWindow(-1, 0, 0, 600, 300, "", $00CF0001)
   
   If WinX
   
      Repeat
      
         GetTime = ElapsedMilliseconds()
      
         OldGInkey = GInkey
      
         GetKeyboard(GetTime) ; Renvoie la chaîne GInkey contenant le buffer des touches appuyées
         
         StartDrawing(WindowOutput(WinX) )

            For y = 0 To 15                 

               For x = 0 To 15                         

                  N = (y << 4) + x

                  xx = (x + 1) << 5
                  yy = (y + 1) << 4
                     
                  DrawText(xx, yy, Str(Keys\Pushed[N])+ "      ") ; Affiche l'état d'appui pour chaque touche

               Next

            Next

         StopDrawing()
         
         If GInkey <> OldGInkey
         
            Title.S + Left(GInkey, 1)
            GInkey = Right(GInkey, Len(GInkey) - 1)
         
            SetWindowTitle(WinX, Title)
         
         EndIf
          
         Delay(1)
         
         eWindow = WindowEvent()

         Select eWindow

            Case #PB_Event_CloseWindow: Exit = $1

         EndSelect

      Until Exit = 1
      
  EndIf

End