Skip to content

Instantly share code, notes, and snippets.

@CougarTown
Created March 30, 2024 21:22
Show Gist options
  • Save CougarTown/244ddbc839d3e76d9a32dc9ad372857b to your computer and use it in GitHub Desktop.
Save CougarTown/244ddbc839d3e76d9a32dc9ad372857b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PS2 Controller Button Detection</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<script>
// PS2 controller button codes
const BUTTON_X = 0x10;
const BUTTON_SQUARE = 0x20;
const BUTTON_CIRCLE = 0x40;
const BUTTON_TRIANGLE = 0x80;
const BUTTON_UP = 0x01;
const BUTTON_DOWN = 0x02;
const BUTTON_LEFT = 0x04;
const BUTTON_RIGHT = 0x08;
const BUTTON_L1 = 0x100;
const BUTTON_L2 = 0x200;
const BUTTON_L3 = 0x400;
const BUTTON_R1 = 0x1000;
const BUTTON_R2 = 0x2000;
const BUTTON_R3 = 0x4000;
const BUTTON_START = 0x8000;
const BUTTON_SELECT = 0x10000;
const BUTTON_ENABLE_ANALOG = 0x20000;
// Function to handle button press
function handleButtonPress(buttonCode) {
// Output decimal and hexadecimal values
console.log(`Button pressed - Decimal: ${buttonCode}, Hexadecimal: 0x${buttonCode.toString(16)}`);
}
// Assume you have a function to get the PS2 controller input
function getPS2ControllerInput() {
// Replace this with actual PS2 controller input retrieval
// Hypothetical function, you should replace it with the real code
// e.g., if you are using a library for PS2 controller input
const ps2Input = navigator.getPS2ControllerInput();
return ps2Input;
}
// Periodically check for button press
setInterval(function() {
const controllerInput = getPS2ControllerInput();
// Handle button press based on the code
if (controllerInput) {
if (controllerInput.buttons.includes('X')) {
handleButtonPress(BUTTON_X);
}
if (controllerInput.buttons.includes('Square')) {
handleButtonPress(BUTTON_SQUARE);
}
if (controllerInput.buttons.includes('Circle')) {
handleButtonPress(BUTTON_CIRCLE);
}
if (controllerInput.buttons.includes('Triangle')) {
handleButtonPress(BUTTON_TRIANGLE);
}
if (controllerInput.buttons.includes('Up')) {
handleButtonPress(BUTTON_UP);
}
if (controllerInput.buttons.includes('Down')) {
handleButtonPress(BUTTON_DOWN);
}
if (controllerInput.buttons.includes('Left')) {
handleButtonPress(BUTTON_LEFT);
}
if (controllerInput.buttons.includes('Right')) {
handleButtonPress(BUTTON_RIGHT);
}
if (controllerInput.buttons.includes('L1')) {
handleButtonPress(BUTTON_L1);
}
if (controllerInput.buttons.includes('L2')) {
handleButtonPress(BUTTON_L2);
}
if (controllerInput.buttons.includes('L3')) {
handleButtonPress(BUTTON_L3);
}
if (controllerInput.buttons.includes('R1')) {
handleButtonPress(BUTTON_R1);
}
if (controllerInput.buttons.includes('R2')) {
handleButtonPress(BUTTON_R2);
}
if (controllerInput.buttons.includes('R3')) {
handleButtonPress(BUTTON_R3);
}
if (controllerInput.buttons.includes('Start')) {
handleButtonPress(BUTTON_START);
}
if (controllerInput.buttons.includes('Select')) {
handleButtonPress(BUTTON_SELECT);
}
if (controllerInput.buttons.includes('EnableAnalog')) {
handleButtonPress(BUTTON_ENABLE_ANALOG);
}
}
}, 1000); // Check every second (adjust as needed)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment