boucle d'evenements et examinekeyboard()

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
ymerdy
Messages : 19
Inscription : jeu. 19/oct./2017 22:10

boucle d'evenements et examinekeyboard()

Message par ymerdy »

Bonjour,

dans l'exemple qui suit, j'ouvre 3 fenetres.
Je souhaite pouvoir quitter l'applicarion par appui sur la touche ESC, mais cela ne fonctionne que si c'est la 3eme fenetre qui a le focus.

Je ne comprends pas pourquoi.
J'ai essayé avec examinekeyboard() placé a differents endroits de la boucle d'evenements sans succés...

Code : Tout sélectionner

  ; Quelques variables
  MargeG = 2 
  MargeH = 2
  LargeurEcran = 440 
  HauteurEcran = 440 
  
Global Window_0, Window_1, Window_2, W2screen, WindowGUI
Global activewin.i
Global Quit.i

Global TrackBar_0, String_0
Global TrackBar_1
Global TrackBar_2


If InitEngine3D()=0
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
  End
EndIf

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
  
InitSprite()
InitKeyboard()
InitMouse()

Procedure OpenWindow_0(x = 0, y = 0, width = 350, height = 220)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  TrackBar_0 = TrackBarGadget(#PB_Any, 30, 30, 280, 40, 0, 100)
  String_0 = StringGadget(#PB_Any, 70, 110, 180, 30, "")
EndProcedure

Procedure OpenWindow_1(x = 400, y = 0, width = 350, height = 220)
  Window_1 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  TrackBar_1 = TrackBarGadget(#PB_Any, 30, 30, 280, 40, 0, 100)
EndProcedure

Procedure OpenWindow_2(x = 800, y = 0, width = 440, height = 440)
  Window_2 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  W2screen=OpenWindowedScreen(WindowID(Window_2), 0, 0, width, height, 0, 0, 0)
  CreateCamera(0, 0, 0, 100, 100)  ; Front camera
  MoveCamera(0,0,100,100, #PB_Absolute)
  WindowGUI=OpenWindow3D(#PB_Any, 10, 10, 340, 200, "trackbar")
  
  ShowGUI(0, 1) ;Hide the GUI, display the mouse cursor
EndProcedure

Procedure Window_1_Events(event)
      tbval.i = GetGadgetState(TrackBar_1)
      SetGadgetState(TrackBar_0,tbval)
      SetGadgetText(String_0,StrU(tbval))
EndProcedure

OpenWindow_0()
OpenWindow_1()
OpenWindow_2()

inScreen = #False
cnt=0
Repeat
  ;Traiter la file d'attente des événements windows
  Repeat
    ; unstack events
    Event = WindowEvent()  ; file d'attente a vider
    EventWindow = EventWindow() ;identification de la fenetre dans laquelle s'est produit l'evenement
    
    ; manage events
    If EventWindow = Window_1
      activewin=1
      Window_1_Events(Event)
    EndIf
    
    If EventWindow = Window_2
      activewin=2
    EndIf
  Until Event = 0
  
  
  Debug "inScreen="+inScreen+" ecranactif="+EcranActif
  ; gestion de la capture souris dans la fenetre 3D
  If inScreen = #True       ; Si la souris est dans l'écran noir...
    ReleaseMouse(#False)    ; ... alors on capture la souris 
    If MouseX() > LargeurEcran-20 Or MouseY() > HauteurEcran-20 Or MouseX() < 10 Or MouseY() <10 
      ReleaseMouse(#True)   ; ...et si elle s'approche des bords de l'écran alors on libère la souris
      inScreen = #False
      Debug "sortie ecran"
    EndIf  
  Else                      
    mx = WindowMouseX(Window_2)      ; Sinon, si la souris entre dans l'écran noir...
    my = WindowMouseY(Window_2)
    Debug "mx="+mx
    If mx < LargeurEcran + MargeG And mx > MargeG And my > MargeH And my < MargeH + HauteurEcran                         
      ReleaseMouse(#False)    ; ... alors on capture la souris 
      MouseLocate(mx-MargeG,my-MargeH) 
      inScreen = #True 
      Debug "entree ecran"
    EndIf 
  EndIf 
  
  
  If inScreen
    If ExamineMouse()
      InputEvent3D(MouseX(), MouseY(), MouseButton(#PB_MouseButton_Left))
    EndIf
    
    If MouseButton(#PB_MouseButton_Left) 
      shininess=(GetGadgetState(TrackBar_1)+1)%80
      SetGadgetText(String_0,StrU(shininess))
      SetGadgetState(TrackBar_0,shininess)
      SetGadgetState(TrackBar_1,shininess)
    EndIf
    
  EndIf
  
  ; Controle des appuis touche
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    Quit=1
  EndIf
  
  
  RenderWorld() 
  FlipBuffers()
  EcranActif=IsScreenActive()
Until Quit=1

Ollivier
Messages : 4197
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Re: boucle d'evenements et examinekeyboard()

Message par Ollivier »

Il te faut une redondance : ExamineKeyboard() c'est pour le screen, et AddKeyboardShortcut() c'est pour les fenêtres. Teste le fonctionnement de cette dernière fonction sans screen d'abord pour comprendre la technique. (Solutions sans API).
ymerdy
Messages : 19
Inscription : jeu. 19/oct./2017 22:10

[RESOLU] boucle d'evenements et examinekeyboard()

Message par ymerdy »

OK, bien compris, ça marche impec.

Merci!
Répondre