From 2443aad01b589cf6b8aaf127d179ff2be632b357 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Tue, 31 Dec 2024 11:58:32 -0500 Subject: [PATCH] key alias and breakout --- packages/core/signal.mjs | 20 ++++++-------------- packages/core/util.mjs | 22 ++++++++++++++++++++++ test/__snapshots__/examples.test.mjs.snap | 4 ++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/core/signal.mjs b/packages/core/signal.mjs index aef1af92..3bf73d8e 100644 --- a/packages/core/signal.mjs +++ b/packages/core/signal.mjs @@ -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, getCurrentKeyboardState } from './util.mjs'; +import { id, isKeyDown } from './util.mjs'; export function steady(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 + * key name reference: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values * - * @name onKey + * @name whenKey * @memberof Pattern * @returns Pattern * @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) { - if (Array.isArray(input) === false) { - input = [input]; - } - - const keyState = getCurrentKeyboardState(); - - const on = input.every((x) => { - return keyState[x]; - }); - return pat.when(on, func); +export const whenKey = register('whenKey', function (input, func, pat) { + return pat.when(isKeyDown(input), func); }); diff --git a/packages/core/util.mjs b/packages/core/util.mjs index 53a017c5..8b623e0b 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -434,6 +434,16 @@ function getUnixTimeSeconds() { 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; export function getCurrentKeyboardState() { @@ -455,6 +465,18 @@ export function getCurrentKeyboardState() { return { ...keyState }; // Return a shallow copy of the key state object } +//keyname: string | Array +//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 diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 33529ac8..3602a4cd 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -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`] = ` [ "[ 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`] = ` [ "[ 0/1 → 1/3 | 10 ]",