Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onKey function for custom key commands for patterns #1235

Merged
merged 9 commits into from
Jan 14, 2025

Conversation

daslyfe
Copy link
Collaborator

@daslyfe daslyfe commented Dec 31, 2024

$: s("bd(5,8)")
.onKey("Control:j", x => x.segment(16).color("red"))
.onKey("Control:i", x => x.fast(2).color("blue"))

@daslyfe daslyfe requested review from yaxu and felixroos December 31, 2024 03:08
@yaxu
Copy link
Member

yaxu commented Dec 31, 2024

Nice! Looks illegal, but not too dangerous :)

Could it support lowercase and short names for modifiers, e.g. control and ctrl?

I think it is worth exposing that on function as well, perhaps as key, to support e.g. key("Control:j")? so it can be used for other things like switching between patterns with sew (although sew still hasn't been ported to strudel yet #31).

Then maybe onKey could be renamed as whenKey as shorthand for when(key(), ...).. (not sure if the shorthand is worthwhile then, but no strong feelings either way..)

@daslyfe
Copy link
Collaborator Author

daslyfe commented Dec 31, 2024

Nice! Looks illegal, but not too dangerous :)

Could it support lowercase and short names for modifiers, e.g. control and ctrl?

I think it is worth exposing that on function as well, perhaps as key, to support e.g. key("Control:j")? so it can be used for other things like switching between patterns with sew (although sew still hasn't been ported to strudel yet #31).

Then maybe onKey could be renamed as whenKey as shorthand for when(key(), ...).. (not sure if the shorthand is worthwhile then, but no strong feelings either way..)

good ideas! updated. I added some common aliases and a link to the key name values in the docs, as well as broke out the function and renamed it

@yaxu
Copy link
Member

yaxu commented Jan 1, 2025

Great thanks!

Looking a bit closer I thought isKeyDown would return a pattern, but now see it returns a single plain value. I'm surprised this works with when, but guess it does because the patternification magic in register is taking care of doing things inside the input pattern so that isKeyDown works on values rather than patterns.

Relying on this magic though means that when(isKeyDown("Control:b"), ...) doesn't work, because isKeyDown doesn't work with patterns.

How about exporting this instead of isKeyDown?

export const keyDown = register('keyDown', (pat) => pat.fmap(isKeyDown));

(the fmap is needed because register doesn't patternify the final parameter..)

Then when(keyDown(... should work, as well as

$: keyDown("Alt:j").pick([s("bd(5,8)"), s("cp(3,8)")]);

@daslyfe
Copy link
Collaborator Author

daslyfe commented Jan 3, 2025

$: keyDown("Alt:j").pick([s("bd(5,8)"), s("cp(3,8)")])

works great! I update the pr

packages/core/signal.mjs Outdated Show resolved Hide resolved
@daslyfe daslyfe merged commit c5de21e into tidalcycles:main Jan 14, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants