Page 1 of 1

Extrawindow - No event from mouse and keyboard

Posted: Wed Aug 06, 2014 4:17 pm
by Bananenfreak
Heyho,

I discovered a mysterious Thing by testing my program. If an other window then your window with WindowedScreen is active (got Focus), you can´t receive Keyboard- and mouseevents. Ok, seems correct (By the way, I don´t want it like this...). But this rule isn´t valid for Joysticks/Gamepads. You can still catch those gamepadevents.
But why is this realised this way? Why we can´t get Events everytime like gamepadevents?

In my case, I implemented Options for custom control. This is in an extra Windows window, but this way I can´t get Keyboard and Mouseevents. I think I have to use CGUI.

Here´s a code for testing:

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Keyboardtest         >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 06.08.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, gamepad
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#N2 = 1


If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  gamepad = InitJoystick()
  If gamepad = 0
    Debug "No gamepad..."
    End
  EndIf
  
  OpenWindow(#window, 0, 0, 1800, 1000, "Keyboardtest", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 2000, 2000, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
  
  AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
  
  CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 0, 20, 0, #PB_Absolute)
  CameraLookAt(#kam_0, 20, 0, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
  
  OpenWindow(#N2, 100, 100, 100, 100,"Test2")
  
  Repeat
    ExamineJoystick(0)
    
    Repeat
    Until WindowEvent() = 0
    
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
      
      If MouseButton(#PB_MouseButton_Left)
        Debug "Mouse"
      EndIf
    EndIf
    
    If ExamineKeyboard()
      
      If KeyboardPushed(#PB_Key_Up)    
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Left)  
        MoveCamera(0, -1 * Boost, 0, 0) 
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf
      
      If KeyboardPushed(#PB_Key_W)
        Debug "Key W"
      EndIf
      
    EndIf
    
    If JoystickButton(0, 1)
      Debug "Gamepad!"
    EndIf
    
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 6:49 am
by applePi
i don't have gamepad or joystick , but what about using SetActiveGadget(#Gadget) somehow before the main loop
at this Example ... without using SetActiveGadget(0) i can't exit using Esc key

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 7:15 am
by Bananenfreak
That's the problem, applepi.
First window got no focus, so no keyboard and mouseevents, but gamepadevents.
My question is: Is this right or is there a problem with keyboard and mouse (or gamepad:-) )?

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 7:26 am
by DK_PETER
Is this what you want?

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Keyboardtest         >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 06.08.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, gamepad
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#N2 = 1


If InitEngine3D()
 
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
  gamepad = InitJoystick()
  If gamepad = 0
    Debug "No gamepad..."
    End
  EndIf
 
  OpenWindow(#window, 0, 0, 1800, 1000, "Keyboardtest", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 2000, 2000, 0, 10, 10, #PB_Screen_SmartSynchronization)
 
  WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
 
  AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
 
  CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
 
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 0, 20, 0, #PB_Absolute)
  CameraLookAt(#kam_0, 20, 0, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
 
  OpenWindow(#N2, 100, 100, 100, 100,"Test2")
  TextGadget(0, 0, 0, 100,100,"")
  StickyWindow(#N2,1)
  Repeat
    ExamineJoystick(0)
   
    Repeat
      If GetActiveWindow() = #N2
        SetActiveWindow(#window)
      EndIf
    Until WindowEvent() = 0
   
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
     
      If MouseButton(#PB_MouseButton_Left)
        SetGadgetText(0,"Mouse left")
      EndIf
    EndIf
   
    If ExamineKeyboard()
     
      If KeyboardPushed(#PB_Key_Up)   
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf
     
      If KeyboardPushed(#PB_Key_Left) 
        MoveCamera(0, -1 * Boost, 0, 0)
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf
     
      If KeyboardPushed(#PB_Key_W)
        SetGadgetText(0,"Key W")
      EndIf
     
    EndIf
   
    If JoystickButton(0, 1)
      SetGadgetText(0,"Button 1")
    EndIf
   
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
   
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 7:34 am
by Bananenfreak
I don´t know if this is caused by my bad english, but I only want a answer on this question:
Is this behavior normal? I mean, a program receives still gamepadevents (If first window is not in front|lost Focus), but no mouse- or keyboardevents.
I don´t understand this behavior, except there are some things with Windows(Microsoft),... .

@DK_PETER:
Thanks, I solved it already this way. But it´s not the nice way :)

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 7:40 am
by DK_PETER
Bananenfreak wrote:I don´t know if this is caused by my bad english, but I only want a answer on this question:
Is this behavior normal? I mean, a program receives still gamepadevents (If first window is not in front|lost Focus), but no mouse- or keyboardevents.
I don´t understand this behavior, except there are some things with Windows(Microsoft),... .

@DK_PETER:
Thanks, I solved it already this way. But it´s not the nice way :)
I would say that it's normal behavior. Gamepads/joysticks reacts to screen only, while mouse/keyboard reacts to windows, which
is why a window must be active to receive mouse/keyboard.

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 9:25 am
by Bananenfreak
Hmm, is there a possibility to switch this off, so I can receive mouse- and keyboardevents even when my window is in background?

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 9:51 am
by DK_PETER
Bananenfreak wrote:Hmm, is there a possibility to switch this off, so I can receive mouse- and keyboardevents even when my window is in background?
I'm not sure, that I follow: What excatly are you trying to do?

Mouse/keyboardevents are only accessible from an active (has focus) window.

If this is not, what you're after - could you please elaborate and explain in detail what you're trying to achieve?

Getting Mouse/keyboardevents from an inactive window makes absolutely no sense to me, if this is what you're after.

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 1:24 pm
by Bananenfreak
I want to get mouse- and keyboardevents from an inactive window.
It makes sense, if you code a harmful program (keylogger) or a funprogram (every pushed key does a sound) or in my case gameoptions for movement,... . I don't want them in a CGUI window.

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 1:34 pm
by heartbone
Bananenfreak wrote:I want to get mouse- and keyboardevents from an inactive window.
Strictly guessing here, my gut instinct tells me that you can probably access the keyboard by interacting with the hardware drivers supporting Windows® or Linux, and I sincerely hope that you really don't want to try this for the mouse.
Besides the experts here, I'm sure there are some internet references on this subject of low level keyboard access.
(3 more days, 14 more posts ;))

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 2:08 pm
by DK_PETER
@Bananenfreak

In your case I would skip the extra window and create a secondary loop for options.
No cgui required..Use sprites in your secondary loop as option buttons sliders or whatever..

Here's a rough example of what I mean. (Use F1 and F2 to switch)

Code: Select all

;Rough example...
EnableExplicit
InitEngine3D()
InitSprite()
InitKeyboard()

Declare.i MakeOptions()
Declare.i MainLoop()
Declare.i SecondLoop()

Global sp.i, First.i, Second.i

OpenWindow(0, 0, 0, 1024, 768, "")
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, 0, 0, 0, #PB_Screen_SmartSynchronization)
CreateCamera(0, 0, 0, 100, 100)

Procedure.i MakeOptions()
  Protected y.i
  sp = CreateSprite(#PB_Any, 1000, 700)
  
  StartDrawing(SpriteOutput(sp))
  DrawingMode(#PB_2DDrawing_Outlined)
  RoundBox(0, 0, 999,699, 20, 20, $FFFF6E)
  For y = 10 To 20
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(200, y * 20, 200, 20, $FFFF6E)
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawText( 20, y * 20, "Option " + Str(y - 10), $FFFF6E)
  Next y
  StopDrawing()
  
  First = CreateSprite(#PB_Any, 100, 20)
  StartDrawing(SpriteOutput(First))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(0,0,"Main loop")
  StopDrawing()
  
  Second = CreateSprite(#PB_Any, 100, 20)
  StartDrawing(SpriteOutput(Second))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(0,0,"Second loop")
  StopDrawing()
  
EndProcedure

Procedure.i MainLoop()
  Protected ev.i, ret.i
  Repeat
    
    Repeat: ev= WindowEvent(): Until ev=0
    
    ExamineKeyboard()  
    
    RenderWorld()
    DisplayTransparentSprite(First, 10,10)
    FlipBuffers()
    
    ;Jump only when updating is done
    If KeyboardReleased(#PB_Key_F1)
      ret = SecondLoop() ;jump to second loop
    EndIf
    
  Until KeyboardPushed(#PB_Key_Escape)
  
EndProcedure

Procedure.i SecondLoop()
  Protected ev.i
  Repeat
    
    Repeat: ev=WindowEvent(): Until ev=0
    
    ExamineKeyboard()  
    
    RenderWorld()
    
    DisplayTransparentSprite(sp,0,0)
    
    DisplayTransparentSprite(Second, 10,10)
    FlipBuffers()
    
  Until KeyboardReleased(#PB_Key_F2) ;Jump back to main loop
  
  ProcedureReturn #True
  
EndProcedure
MakeOptions()
MainLoop()

Re: Extrawindow - No event from mouse and keyboard

Posted: Thu Aug 07, 2014 3:59 pm
by Bananenfreak
Bananenfreak wrote:I want to get mouse- and keyboardevents from an inactive window.
Sry, this sentence makes not really sense. I want to get mouse- and keyboardevents even if my window is inactive.

I think I can´t get the solution I want, so I take DK_PETERs code.

@DK_PETER:
Good, thanks :) I think I use your code in my program.

Re: Extrawindow - No event from mouse and keyboard

Posted: Fri Aug 08, 2014 7:10 pm
by Mythros
What about GetASyncKeyState_()?

Re: Extrawindow - No event from mouse and keyboard

Posted: Sat Aug 09, 2014 8:04 am
by Bananenfreak
Could also be a possible solution (I used this also for mouse in my program), thanks.