Mouse in screen

Advanced game related topics
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

The program I'm working on will require the default computer's cursor, otherwise I wouldn't. :)

Here's the code for the openscreen areas:

Code: Select all

         If OpenScreen(tempWidth,tempHeight,DesktopDepth(0),program$)
            ShowCursor_(1)
            SetClassLong_(ScreenID(),#GCL_HCURSOR,LoadCursor_(0,#IDC_ARROW))
            
            windowSprite = CreateSprite(#PB_Any,tempWidth,tempHeight)
            ;mouseSprite = LoadSprite(#PB_Any,"cursor.bmp")
            ;TransparentSpriteColor(mouseSprite,RGB(128,128,128))
            
..... when the screen closes:

Code: Select all

              ;ExamineMouse()
              ;DisplayTransparentSprite(mouseSprite,MouseX(),MouseY())
              
              FlipBuffers()
              
              ExamineKeyboard()
              Delay(10)
            Until KeyboardPushed(#PB_Key_Grave) And KeyboardPushed(#PB_Key_Escape)
            
            FreeSprite(windowSprite)
            ;FreeSprite(mouseSprite)
            CloseScreen()
            ShowCursor_(0)

Another question:
Should Flip Buffers be at the top of my loop, and ClearScreen at the bottom, or FlipBuffers at the bottom and ClearScreen at the top?
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

hmm I put a delay(5000) before the windowSprite = CreateSprite(#PB_Any,tempWidth,tempHeight) line, and the mouse showed for 5 seconds.... then I changed the size of the sprite, and the mouse still didn't show, so I don't believe it has to do with the sprite... hmm...

anyideas why?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Now I understand. InitMouse() / ExamineMouse() are the problem here. These commands use DirectInput and are superior to the regular input. No chance to get around this but you could still rely on standard mouse input:

Code: Select all

InitSprite() : InitKeyboard()

OpenScreen(640,480,16,"untitled")
ShowCursor_(1)
SetClassLong_(ScreenID(),#GCL_HCURSOR,LoadCursor_(0,#IDC_ARROW))

Repeat   
	ClearScreen($854422)   
	
	ExamineKeyboard() 
	
	StartDrawing(ScreenOutput())   
	If GetAsyncKeyState_(#VK_LBUTTON)
		DrawText(10,10,"MOUSE INPUT: LEFT BUTTON")	
	ElseIf GetAsyncKeyState_(#VK_MBUTTON)
		DrawText(10,10,"MOUSE INPUT: MIDDLE BUTTON")
	ElseIf GetAsyncKeyState_(#VK_RBUTTON)
		DrawText(10,10,"MOUSE INPUT: RIGHT BUTTON")
	Else
		DrawText(10,10,"MOUSE INPUT: IDLE")
	EndIf
	
	GetCursorPos_(cpt.POINT)	
	DrawText(10,40,"MOUSE POSITION: " + Str(cpt\x) + "," + Str(cpt\y))
	StopDrawing()
	
	FlipBuffers()
Until KeyboardPushed(1)
Last edited by Fluid Byte on Sun Feb 04, 2007 3:29 am, edited 3 times in total.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

-edit-

ok I understand.
Thank You! :)
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

I tried doing that, the mouse shows and everything, but it lags... and my code no longer works trying to figure out if the mouse was pressed or released (check my other mouse post for my code)
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Code: Select all

InitSprite()

InitKeyboard()
OpenWindow(0,0,0,800,200,"")
OpenWindowedScreen(WindowID(0),0,0,800,200,0,0,0)
ticks=0
flag=0
pressed=0
ExamineKeyboard()

While Not KeyboardPushed(#PB_Key_Escape)
 WindowEvent()
  Delay(1)


  If GetAsyncKeyState_(#VK_LBUTTON) 
    ticks+1
    flag=1

    If ticks >= 200 And pressed = 0
      Debug "left pressed"
      pressed = 1
    EndIf
  EndIf

  If GetAsyncKeyState_(#VK_LBUTTON)=0
    If flag=1
      If ticks<200
        Debug "left clicked"
      Else
        Debug "left released"
      EndIf
      flag=0
      ticks=0
      pressed = 0
    EndIf
  EndIf

  ExamineKeyboard()
Wend 
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

This is the two programs merged together, you won't see the debug output until you quit, then you will see that it was working.

Not sure about the lag that you mentioned, can you explain more, is the cursor juddery or something?

Code: Select all

InitSprite() : InitKeyboard()

OpenScreen(640,480,16,"untitled")
ShowCursor_(1)
SetClassLong_(ScreenID(),#GCL_HCURSOR,LoadCursor_(0,#IDC_ARROW))

ticks=0
flag=0
pressed=0
Repeat
  ClearScreen($854422)

  ExamineKeyboard()

  StartDrawing(ScreenOutput())

  If GetAsyncKeyState_(#VK_LBUTTON)
    ticks+1
    flag=1

    If ticks >= 200 And pressed = 0
      Debug "left pressed"
      pressed = 1
    EndIf
  EndIf

  If Not GetAsyncKeyState_(#VK_LBUTTON)
    If flag=1
      If ticks<200
        Debug "left clicked"
      Else
        Debug "left released"
      EndIf
      flag=0
      ticks=0
      pressed = 0
    EndIf
  EndIf

  GetCursorPos_(cpt.POINT)
  DrawText(10,40,"MOUSE POSITION: " + Str(cpt\x) + "," + Str(cpt\y))
  StopDrawing()

  FlipBuffers()
Until KeyboardPushed(1)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

When you check GetAsyncKeystate_() you should test logical AND with 32768 as that bit is the one that signifies key down or not. There are situations where other bits could be set with the key up, and if you're just checking If GetAsyncKeystate(#key) you can get an incorrect result of the test. This code is recommended:

Code: Select all

If GetAsyncKeyState_(#VK_LBUTTON) & 32768
  ; key is down
Else
  ; key is up
EndIf
BERESHEIT
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Okay, using GetAsyncKeyState_(#VK_LBUTTON) & 32768 to check when the mouse is down and using Not GetAsyncKeyState_(#VK_LBUTTON) & 32768 to check when it's not down works.
In my program however, as I said, the mouse judders.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

It's not going to fix your judder, it's just the right way to use the GetAsyncKeyState_() if you're testing a key down or up, that's all.
BERESHEIT
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Oh I know that,
I was stating that the lag I mentioned earlier is a judder kind of effect.
It doesn't happen when I use the mousebutton function, only the getasynckeystate....
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

what do you mean by "judder"?

take a look at my WinScreenMouse example.
http://www.purebasic.fr/english/viewtopic.php?t=25082
I'm using GetAsyncKeystate for the buttons, even tho I use an own mousepointer within the screen.
does the effekt happen there, too?
Last edited by Kaeru Gaman on Tue Feb 06, 2007 11:42 am, edited 1 time in total.
oh... and have a nice day.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

No, it does not. Maybe I should post my code?
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Yeah, if you post your code then we can take a look and try and fix it for you, if possible.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Code: Select all

          If OpenScreen(tempWidth,tempHeight,DesktopDepth(0),program$+" Control Client")
            ShowCursor_(1)
            SetClassLong_(ScreenID(),#GCL_HCURSOR,LoadCursor_(0,#IDC_ARROW))
            
            windowSprite = CreateSprite(#PB_Any,tempWidth,tempHeight)
            ;mouseSprite = LoadSprite(#PB_Any,"cursor.bmp")
            ;TransparentSpriteColor(mouseSprite,RGB(128,128,128))
              
              ;Disable Alt-Tab and Alt-ESC
              hooklib = OpenLibrary(0,"AltHook.dll")
              If hooklib
                keyproc=GetFunction(0, "KeyboardProc")
                KeyboardHook=SetWindowsHookEx_(#WH_KEYBOARD_LL, keyproc, hooklib, 0)
                CallFunction(0, "InitHook", KeyboardHook)
              EndIf
              
            leftMouseTicks = 0
            leftMouseFlag = 0
            leftMousepressed = 0
            rightMouseTicks = 0
            rightMouseFlag = 0
            rightMousePressed = 0

            Repeat

              ClearScreen(RGB(0,0,0))
              
              ;ExamineMouse()
              ;tempMouseX = MouseX()
              ;tempMouseY = MouseY()
              GetCursorPos_(tempMousePos.POINT)
              tempMouseX = tempMousePos\x
              tempMouseY = tempMousePos\y
              SendNetworkString(tempClient,"MOU|"+Str(tempMouseX)+"|"+Str(tempMouseY))
              
              ;ExamineMouse()
              If GetAsyncKeyState_(#VK_LBUTTON) & 32768
                leftMouseTicks = leftMouseTicks + 1
                leftMouseFlag = 1
                If leftMouseTicks >= totalTicks And leftMousePressed = 0
                  Debug "left pressed"
                  leftMousePressed = 1
                  SendNetworkString(tempClient,"SIM|STARTDRAG")
                EndIf
              EndIf
              If Not GetAsyncKeyState_(#VK_LBUTTON) & 32768
                If leftMouseFlag = 1
                  If leftMouseTicks < totalTicks
                    Debug "left clicked"
                    SendNetworkString(tempClient,"SIM|LEFTCLICK")
                  Else
                    Debug "left released"
                    SendNetworkString(tempClient,"SIM|ENDDRAG")
                  EndIf
                  leftMouseFlag = 0
                  leftMouseTicks = 0
                  leftMousePressed = 0
                EndIf
              EndIf
              
              If GetAsyncKeyState_(#VK_LBUTTON) & 32768
                rightMouseTicks = rightMouseTicks + 1
                rightMouseFlag = 1
                If rightMouseTicks >= totalTicks And rightMousePressed = 0
                  ;Debug "right pressed"
                  rightMousePressed = 1
                EndIf
              EndIf
              If Not GetAsyncKeyState_(#VK_LBUTTON) & 32768
                If rightMouseFlag = 1
                  If rightMouseTicks < totalTicks
                    ;Debug "right clicked"
                    SendNetworkString(tempClient,"SIM|RIGHTCLICK")
                  Else
                    ;Debug "right released"
                  EndIf
                  rightMouseFlag = 0
                  rightMouseTicks = 0
                  rightMousePressed = 0
                EndIf
              EndIf
              
              
              SEvent = NetworkServerEvent()
              
                If SEvent
                
                  TheSever = EventServer()
                  ClientID = EventClient()
                  ClientIP = GetClientIP(ClientID)
                  
                  Select SEvent
                  
                    Case #PB_NetworkEvent_Connect ;Client connected
                    
                    Case #PB_NetworkEvent_Data ;Raw data received
                      ;A new client has connected, so add him to the client list
                        If ClientID <> tempClient
                          ReceiveNetworkData(ClientID,Buffer,5000)
                          AddGadgetItem(#clientList,-1,PeekS(Buffer)+Chr(10)+Str(ClientID))
                        EndIf
                      
                    Case #PB_NetworkEvent_File ;File received
                      
                    Case #PB_NetworkEvent_Disconnect ;Client disconnected
                      
                  EndSelect
                
                  FreeMemory(Buffer)
                  Buffer = AllocateMemory(5000)
                
                EndIf
             
              
              DisplaySprite(windowSprite,0,0)
              
              ;ExamineMouse()
              ;DisplayTransparentSprite(mouseSprite,MouseX(),MouseY())
              
              FlipBuffers()
                            
              ExamineKeyboard()
              Delay(10)
            Until KeyboardPushed(#PB_Key_Grave) And KeyboardPushed(#PB_Key_Escape)
            
            FreeSprite(windowSprite)
            ;FreeSprite(mouseSprite)
            CloseScreen()
            ShowCursor_(0)
            UnhookWindowsHookEx_(KeyboardHook)
            SendNetworkString(tempClient,"OFF|ControlOff")
          EndIf
Post Reply