Skip to content

Instantly share code, notes, and snippets.

@boppreh
Created May 4, 2015 21:47
Show Gist options
  • Save boppreh/6273606f3fdc5fe07c47 to your computer and use it in GitHub Desktop.
Save boppreh/6273606f3fdc5fe07c47 to your computer and use it in GitHub Desktop.
Allow keyboard control in agar.io
var oldOnKeyDown = window.onkeydown
var canvas = document.getElementById('canvas');
var oldMouseMove = canvas.onmousemove
canvas.onmousemove = null;
var pressedKeys = {}
window.onkeydown = function (f) {
pressedKeys[f.keyCode] = 1;
oldOnKeyDown(f);
updateKey();
};
window.onkeyup = function (f) {
pressedKeys[f.keyCode] = 0;
updateKey();
};
function updateKey() {
var maxX = window.innerWidth;
var maxY = window.innerHeight;
var dx = 0;
var dy = 0;
if (pressedKeys[37]) {
dx = -1;
}
if (pressedKeys[38]) {
dy = -1;
}
if (pressedKeys[39]) {
dx = 1;
}
if (pressedKeys[40]) {
dy = 1;
}
var x, y;
if (dx == -1) {
x = 0;
} else if (dx == 1) {
x = maxX;
} else if (dx === 0) {
x = maxX / 2;
}
if (dy == -1) {
y = 0;
} else if (dy == 1) {
y = maxY;
} else if (dy === 0) {
y = maxY / 2;
}
oldMouseMove({clientX: x, clientY: y});
}
@Ruukas97
Copy link

How do I use this?
Edit: nvm I found it

@BogdanGlisici
Copy link

How do I use this? XD

@boppreh
Copy link
Author

boppreh commented Feb 18, 2022

@BogdanGlisici It's mean to pasted on the console. But you really, really should not do that unless you fully understand the code you're pasting. It's the javascript equivalent of letting someone in your house (or website account, in this case).

@BogdanGlisici
Copy link

BogdanGlisici commented Feb 18, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment