Extrawindow - No event from mouse and keyboard

Everything related to 3D programming
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Extrawindow - No event from mouse and keyboard

Post 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
Last edited by Bananenfreak on Thu Aug 07, 2014 7:19 am, edited 1 time in total.
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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:-) )?
Image
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Extrawindow - No event from mouse and keyboard

Post 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
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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 :)
Image
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Extrawindow - No event from mouse and keyboard

Post 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.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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?
Image
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Extrawindow - No event from mouse and keyboard

Post 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.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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.
Image
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Extrawindow - No event from mouse and keyboard

Post 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 ;))
Keep it BASIC.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Extrawindow - No event from mouse and keyboard

Post 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()
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post 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.
Image
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Extrawindow - No event from mouse and keyboard

Post by Mythros »

What about GetASyncKeyState_()?
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Extrawindow - No event from mouse and keyboard

Post by Bananenfreak »

Could also be a possible solution (I used this also for mouse in my program), thanks.
Image
Post Reply