# Extending the mouse range past the screen size

**URL:** <https://discourse.processing.org/t/extending-the-mouse-range-past-the-screen-size/4431>\
**Category:** Coding Questions\
**Created:** [October 13, 2018, 4:53pm UTC](https://discourse.processing.org/t/extending-the-mouse-range-past-the-screen-size/4431 "2018-10-13T16:53:31Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [October 13, 2018, 6:46pm UTC](https://discourse.processing.org/t/extending-the-mouse-range-past-the-screen-size/4431/3 "2018-10-13T18:46:47Z")

</div>

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

```auto
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](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)_

---

_[View the full topic](https://discourse.processing.org/t/extending-the-mouse-range-past-the-screen-size/4431)._
