Skip to content

Commit

Permalink
_keyDown
Browse files Browse the repository at this point in the history
  • Loading branch information
daslyfe committed Jan 14, 2025
1 parent 052addd commit 570c7a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 16 additions & 3 deletions packages/core/signal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Hap } from './hap.mjs';
import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs';
import Fraction from './fraction.mjs';

import { id, isKeyDown } from './util.mjs';
import { id, keyAlias, getCurrentKeyboardState } from './util.mjs';

export function steady(value) {
// A continuous value
Expand Down Expand Up @@ -641,6 +641,19 @@ export const always = register('always', function (func, pat) {
return func(pat);
});

//keyname: string | Array<string>
//keyname reference: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
export function _keyDown(keyname) {
if (Array.isArray(keyname) === false) {
keyname = [keyname];
}
const keyState = getCurrentKeyboardState();
return keyname.every((x) => {
const keyName = keyAlias.get(x) ?? x;
return keyState[keyName];
});
}

/**
*
* Do something on a keypress, or array of keypresses
Expand All @@ -654,7 +667,7 @@ export const always = register('always', function (func, pat) {
*/

export const whenKey = register('whenKey', function (input, func, pat) {
return pat.when(keyDown(input), func);
return pat.when(_keyDown(input), func);
});

/**
Expand All @@ -670,5 +683,5 @@ export const whenKey = register('whenKey', function (input, func, pat) {
*/

export const keyDown = register('keyDown', function (pat) {
return pat.fmap(isKeyDown);
return pat.fmap(_keyDown);
});
13 changes: 1 addition & 12 deletions packages/core/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,7 @@ export function getCurrentKeyboardState() {

return { ...keyState }; // Return a shallow copy of the key state object
}
//keyname: string | Array<string>
//keyname reference: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
export function isKeyDown(keyname) {
if (Array.isArray(keyname) === false) {
keyname = [keyname];
}
const keyState = getCurrentKeyboardState();
return keyname.every((x) => {
const keyName = keyAlias.get(x) ?? x;
return keyState[keyName];
});
}


// Floating point versions, see Fraction for rational versions
// // greatest common divisor
Expand Down

0 comments on commit 570c7a0

Please sign in to comment.