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

Key Press API #4

Open
Sherlouk opened this issue Nov 28, 2018 · 2 comments
Open

Key Press API #4

Sherlouk opened this issue Nov 28, 2018 · 2 comments
Labels
enhancement New feature or request flesh out Issue details are incomplete and need expanding upon

Comments

@Sherlouk
Copy link
Owner

One of the core fundamentals of this library is going to be the ability for developers to pick up on key presses and do something with that action. I haven't completely determined how I'd like this to work though!

Here are the different things I want to be able to track:

  • Key(s) Down
  • Key(s) Up
  • Key(s) Hold (for a specified amount of time)

Each of these should have the ability to specify either one or multiple keys to listen for. For example I could say when key 1 is pressed down do something, or when both keys 1, 3, and 5 are all pressed at the same time do something else.

Key hold is an interesting one and I don't believe should be exclusive from other events (in that it will still call key up after the specified time) and should be called once as soon as the time has been reached.

@Sherlouk Sherlouk added enhancement New feature or request flesh out Issue details are incomplete and need expanding upon labels Nov 28, 2018
@Sherlouk
Copy link
Owner Author

Considered patterns so far:

  • "Listener" (Similar to NotificationCenter) where the client would add/remove observers from the main/key classes with an event type (from an enum). When data comes in we simply loop through all of the active listener, filter on those which are applicable, and send out the relevant information to the selector/block provided.
  • Delegate (Fairly common) where the client would specify itself as the target and would have three functions for keyDown, keyUp and keyHold (time is passed through, and is sent every second until released) this removes the need for a client to add multiple listeners.

@Sherlouk
Copy link
Owner Author

Example Usage:

let keyOne = ...
let keyTwo = ...

let eventObserver = keyOne.onKeyDown {
    // Key down
}

keyOne.onKeyUp {
    // Key up
}

keyOne.onKeyHold(for: 1) {
     // Key held for 1 second
}

[keyOne, keyTwo].onKeysDown {
    // Both keys are down
}

[keyOne, keyTwo].onKeysHold(for: 1) {
    // Both keys have been held down for 1 second
}

eventObserver.clear() // Clears single listener
keyOne.clearListeners() // Clears all listeners exclusively on a single key (not multi key ones)
streamDeck.clearAllListeners() // Clears all listeners

onKeysUp for multiple keys I believe is going to be a bit more complicated but could fit within the same idea. (Would need to track that both keys have been put down first, and then both released)

All functions will return a token which can be used to remove it at a later date to stop it tracking. This can be ignored though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request flesh out Issue details are incomplete and need expanding upon
Projects
None yet
Development

No branches or pull requests

1 participant