Skip to content

Commit

Permalink
getpressed
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiljevic committed Aug 20, 2019
1 parent b9eba1d commit 1c487ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions pygame/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ var createKeyboardEvent = function (event) {

function keyEventListener(event) {
var e = createKeyboardEvent(event);
if(e[0] == PygameLib.constants.KEYDOWN)
PygameLib.pressedKeys[e[1].key] = true
else if ((e[0] == PygameLib.constants.KEYUP))
delete PygameLib.pressedKeys[e[1].key]
if (PygameLib.eventQueue) {
if (PygameLib.repeatKeys) {
PygameLib.eventQueue.unshift(e);
Expand Down Expand Up @@ -1091,6 +1095,7 @@ function pygame_init() {
pygame_m.$d['draw'] = display_m.$d['draw'];
pygame_m.$d['image'] = display_m.$d['image'];
PygameLib.eventQueue = [];
PygameLib.pressedKeys = {}
PygameLib.eventTimer = {};
PygameLib.running = true;
PygameLib.repeatKeys = false;
Expand All @@ -1113,21 +1118,21 @@ var mouseEventListener = function (event) {
canvasX = event.clientX - totalOffsetX;
canvasY = event.clientY - totalOffsetY;

var button = event.button + 1;
var button = event.button;
if (event.type === "mousedown") {
var e = [PygameLib.constants.MOUSEBUTTONDOWN,
{
key: PygameLib.constants.MOUSEBUTTONDOWN,
pos: [canvasX, canvasY],
button: button
button: button + 1
}];
PygameLib.mouseData["button"][button] = 1;
} else if (event.type === "mouseup") {
var e = [PygameLib.constants.MOUSEBUTTONUP,
{
key: PygameLib.constants.MOUSEBUTTONUP,
pos: [canvasX, canvasY],
button: button
button: button + 1
}];
PygameLib.mouseData["button"][button] = 0;
} else if (event.type === "mousemove") {
Expand Down
7 changes: 4 additions & 3 deletions pygame/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ var $builtinmodule = function (name) {
return Sk.ffi.remapToPy(document.hasFocus());
});
mod.get_pressed = new Sk.builtin.func(function () {
var pressed = new Array(323).fill(false);
for (var i = 0; i < PygameLib.eventQueue.length; i++) {
pressed[PygameLib.eventQueue[i][1].key] = true;
var pressed = new Array(PygameLib.constants.K_LAST+1).fill(false);
for(var key = 0; key< pressed.length; key ++) {
if(PygameLib.pressedKeys[key])
pressed[key] = true
}
return Sk.ffi.remapToPy(pressed);
});
Expand Down

0 comments on commit 1c487ca

Please sign in to comment.