key alias and breakout

This commit is contained in:
Jade (Rose) Rowland 2024-12-31 11:58:32 -05:00
parent 08f6c1ad5e
commit 2443aad01b
3 changed files with 30 additions and 16 deletions

View File

@ -8,7 +8,7 @@ import { Hap } from './hap.mjs';
import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs'; import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs';
import Fraction from './fraction.mjs'; import Fraction from './fraction.mjs';
import { id, getCurrentKeyboardState } from './util.mjs'; import { id, isKeyDown } from './util.mjs';
export function steady(value) { export function steady(value) {
// A continuous value // A continuous value
@ -644,23 +644,15 @@ export const always = register('always', function (func, pat) {
/** /**
* *
* Do something on a keypress, or array of keypresses * Do something on a keypress, or array of keypresses
* key name reference: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
* *
* @name onKey * @name whenKey
* @memberof Pattern * @memberof Pattern
* @returns Pattern * @returns Pattern
* @example * @example
* s("bd(5,8)").onKey("Control:j", x => x.segment(16).color("red")).onKey("Control:i", x => x.fast(2).color("blue")) * s("bd(5,8)").whenKey("Control:j", x => x.segment(16).color("red")).whenKey("Control:i", x => x.fast(2).color("blue"))
*/ */
export const onKey = register('onKey', function (input, func, pat) { export const whenKey = register('whenKey', function (input, func, pat) {
if (Array.isArray(input) === false) { return pat.when(isKeyDown(input), func);
input = [input];
}
const keyState = getCurrentKeyboardState();
const on = input.every((x) => {
return keyState[x];
});
return pat.when(on, func);
}); });

View File

@ -434,6 +434,16 @@ function getUnixTimeSeconds() {
return Date.now() * 0.001; return Date.now() * 0.001;
} }
export const keyAlias = new Map([
['control', 'Control'],
['ctrl', 'Control'],
['alt', 'Alt'],
['shift', 'Shift'],
['down', 'ArrowDown'],
['up', 'ArrowUp'],
['left', 'ArrowLeft'],
['right', 'ArrowRight'],
]);
let keyState; let keyState;
export function getCurrentKeyboardState() { export function getCurrentKeyboardState() {
@ -455,6 +465,18 @@ export function getCurrentKeyboardState() {
return { ...keyState }; // Return a shallow copy of the key state object 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 // Floating point versions, see Fraction for rational versions
// // greatest common divisor // // greatest common divisor

View File

@ -4902,8 +4902,6 @@ exports[`runs examples > example "often" example index 0 1`] = `
] ]
`; `;
exports[`runs examples > example "onKey" example index 0 1`] = `[]`;
exports[`runs examples > example "orbit" example index 0 1`] = ` exports[`runs examples > example "orbit" example index 0 1`] = `
[ [
"[ 0/1 → 1/6 | s:hh delay:0.5 delaytime:0.25 orbit:1 ]", "[ 0/1 → 1/6 | s:hh delay:0.5 delaytime:0.25 orbit:1 ]",
@ -8834,6 +8832,8 @@ exports[`runs examples > example "when" example index 0 1`] = `
] ]
`; `;
exports[`runs examples > example "whenKey" example index 0 1`] = `[]`;
exports[`runs examples > example "withValue" example index 0 1`] = ` exports[`runs examples > example "withValue" example index 0 1`] = `
[ [
"[ 0/1 → 1/3 | 10 ]", "[ 0/1 → 1/3 | 10 ]",