Extending the mouse range past the screen size

Try this sketch! It should do everything perfectly regardless of windowed or fullscreen. I guess you could try integrating its code into your code?

import com.jogamp.newt.opengl.GLWindow;
GLWindow r;
float posX=100;
float posY=100;
boolean Lock=false;
int offsetX=0;
int offsetY=0;           //If we lock the pointer, it will be teleported 
                         //to the center of the screen only as soon as 
                         //the draw() function ends, and until then the
                         //position value will be on a random spot
                         //where the user clicked.
                         //If we take into account that spot for that
                         //one frame, we can correct the position.

float Sensitivity=0.5;

void setup(){
  r=(GLWindow)surface.getNative(); //Ooh, complex OpenGL things I don't know much about myself.
  size(640,480,P2D);
  surface.setResizable(true); //I like resizable surfaces.
}


void draw(){
  
  
  if(Lock){posX+=(mouseX-offsetX-width/2)*Sensitivity; posY+=(mouseY-offsetY-height/2)*Sensitivity;} //If lock, then adjust position...
  //Any random code that depends on those positions...
  
  background(0);
  
  fill(192);
  ellipse(posX,posY,20,20);
  
  fill(255);
  text(Lock?"Locked":"Unlocked",2,15);
  text("posX: "+posX+"\nposY: "+posY,2,35);
  
  
  
  
  if(Lock){
    offsetX=offsetY=0;
  r.setPointerVisible(false); //When locked and trying to move, the pointer jerks all over the place, so best to hide it.
  r.warpPointer(width/2,height/2); //Move it to the exact center of the sketch window.
  r.confinePointer(true); //Locks pointer inside of the sketch's window so it doesn't escape.
                          //The pointer will still hit the window's edges, limiting moves fast enough.
                          //But as soon as the pointer is outside of the window it's visible until it's
                          //teleported back inside the window, which is less preferable.
                          
  }else{r.confinePointer(false); r.setPointerVisible(true);} //Else undo that stuff.
  
  
}



void mousePressed(){
  Lock=!Lock;
  if(Lock){
    offsetX=mouseX-width/2; offsetY=mouseY-height/2;
  }else{
    r.warpPointer(round(posX),round(posY)); //Teleport it to an expected position. Handier that way!
  }
  
}

void keyPressed(){
  if(key=='r'){posX=posY=50;}
}

Also, this might be a bad idea that would confuse everyone including you, but I’ve made some random messy code that allows you to navigate around 3D space similar to what’s in games (like you’re flying around in “noclip” mode, if you know what I mean), and here it is: https://pastebin.com/J4wnp6e2

And yes, I did break my head a couple of times there and made some stuff overly complicated, so please don’t question it! ._.

(also that project of mine ended up having a bunch of unrelated functions in it that I pasted in from my other projects, so, uh, have that for free too, I guess? lol)

2 Likes