Input Management
//addEventListener(type, listener)
//addEventListener(type, listener, options)
window.addEventListener('keyup', inputManagementFunction);
window.addEventListener('keydown', inputManagementFunction);
console.log(window)
Window file:///home/co/OneDrive/2023_24/1_UI-Programming/UI-Programming-23-24-LABS/04-eventListeners-Input-Control/0-inputManagement/index.html
Note that when the specified event is triggered or heard, we don’t
exactly call a function, we refer to a listener, which can
be understood like a function call, so long as we handle the event that
is passed.
window.addEventListener('keyup', inputManagementFunction);
window.addEventListener('keydown', inputManagementFunction);
function ManagementFunction(event) {
console.log("Key: " + event.key);
if (event.type === "keydown") {
switch (event.key) {
case "ArrowLeft":
console.log("left arrow pressed");
moveLeft();
break;
case "ArrowUp":
moveUp();
break;
default:
stopMovement();
}
}
}
let xPos = 0;
let yPos = 0;
let width = 100;
let height = 100;
function moveUp(){
context.clearRect(0,0, canvas.width, canvas.height);
yPos++;
context.fillRect(xPos, yPos, width, heigth);
}
<table>
<tbody>
<tr>
<td> </td>
<td><button onmousedown="clickDpadYellow()" onmouseup="clickableDpadReleased()" class="button yellow round">Y</button></td>
<td> </td>
</tr>
<tr>
<td><button onmousedown="clickDpadBlue()" onmouseup="clickableDpadReleased()" class="button blue round">X</button></td>
<td> </td>
<td><button onmousedown="clickDpadRed()" onmouseup="clickableDpadReleased()" class="button red round">B</button></td>
</tr>
<tr>
<td> </td>
<td><button onmousedown="clickDpadGreen()" onmouseup="clickableDpadReleased()" class="button green round">A</button></td>
<td> </td>
</tr>
<tr>
</tbody>
</table>
CSS and JS to give visual
feedback.pressed{
box-shadow: 0 5px #3D3D3D;
transform: translateY(5px);
}
greenButton.classList.add("pressed");
greenButton.classList.remove("pressed");
const canvas = document.getElementById("the_canvas");
const context = canvas.getContext("2d");
let width = 100;
let height = 20;
let max = 100;
let val = 100;
function drawTimer() {
// Draw the background
context.fillStyle = "#000000";
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillRect(0, 0, width, height);
// Draw the fill
context.fillStyle = "#00FF00";
var fillVal = Math.min(Math.max(val / max, 0), 1);
context.fillRect(0, 0, fillVal * width, height);
// Decrease the fill
val = val - 1;
}
setInterval(function(){
console.log("decreasing val");
drawTimer();
}, 1000);