This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ElvishJerricco's profile picture

Another Idea For Keyboard Shortcuts

Started by ElvishJerricco, 30 August 2013 - 03:02 PM
ElvishJerricco #1
Posted 30 August 2013 - 05:02 PM
I know keyboard shortcuts have been suggested a million times before. But the reason they're denied is because they usually involve some extra key events that would end up sending at least two or three times as many events for every key press. What I'm asking for is the ability to register for shortcuts. Instead of being notified every time a key is pressed and released, a program just registers a shortcut event. Something like:


local identifier = os.registerShortcut(keys.leftCtrl, keys.a)
local event, id = os.pullEvent("key_shortcut")
if id == identifier then
	print("Ctrl-A pressed")
end

The registerShortcut function takes a variable number of arguments. There must be at least one modifier key and only one non-modifier key. Once one of the modifier keys is pressed, the modifiers currently pressed are tracked by the client until a non-modifier is pressed. If the key combo pressed matches the registered shortcut, the server is notified of the shortcut being pressed which issues the event to the Lua environment.

Also, perhaps in order to allow right/left ctrl equivalency (or any other key that appears twice on the keyboard), you can pass a table in place of a key argument, containing all equivalent keys.


local identifier = os.registerShortcut( { keys.leftCtrl, keys.rightCtrl }, keys.a)
local event, id = os.pullEvent("key_shortcut")
if id == identifier then
	print("Ctrl-A pressed")
end

This way you don't have to register a shortcut for every possible combination of left/right versions of desired keys.

EDIT: One more thing, if the same keyboard shortcut is registered twice, two events are issued. One for each shortcut identifier. Can't think of a better way around this problem…

EDIT2: Actually, the simple solution would be returning the identifier from the first shortcut when the second one is registered. And then, if two shortcuts can be the same, but might not, two identifiers are made and events for both are sent. So a {leftCtrl,rightCtrl}+a and a leftCtrl+a wouldn't share an identifier, but if leftCtrl+a was pressed, an event for both identifiers would be sent. While if {leftCtrl,rightCtrl}+a is registered twice (there'd need to be some logic programmed to determine equivalency in case the tables were in a different order or something), both registrations return the same identifier.
Sora Firestorm #2
Posted 30 August 2013 - 11:31 PM
This sounds like a great idea. It's been well thought out and has a good API call to make it easily usable. +1
jay5476 #3
Posted 31 August 2013 - 12:04 AM
definetly a good idea although I believe it is possible with the use of coroutines
ElvishJerricco #4
Posted 31 August 2013 - 01:48 AM
definetly a good idea although I believe it is possible with the use of coroutines
It's sort of possible. You can't track which keys are still down. So pressing ctrl, releasing ctrl, then pressing a would still push the ctrl+a shortcut. Plus it still pushes the a key and char events.
Lyqyd #5
Posted 31 August 2013 - 03:12 AM
IIRC, pressing a character-generating key while the Ctrl key is being held only generates a key event, so it is possible to know that Ctrl was held at the time of the keypress already.
ElvishJerricco #6
Posted 31 August 2013 - 09:14 AM
IIRC, pressing a character-generating key while the Ctrl key is being held only generates a key event, so it is possible to know that Ctrl was held at the time of the keypress already.

Interesting and very useful. But not quite as comprehensive as my suggestion. Although it does make it less necessary.
Engineer #7
Posted 01 September 2013 - 08:51 PM
definetly a good idea although I believe it is possible with the use of coroutines
It's sort of possible. You can't track which keys are still down. So pressing ctrl, releasing ctrl, then pressing a would still push the ctrl+a shortcut. Plus it still pushes the a key and char events.
If you have a proper event loop and some magic with timers, it is very possible to do this within lua.
ElvishJerricco #8
Posted 01 September 2013 - 09:26 PM
If you have a proper event loop and some magic with timers, it is very possible to do this within lua.

How exactly? You can't detect when the keys are released, so you can't know if a modifier key is down when the character is pressed. The best you can do is detect ctrl+<char-key> because the char event isn't pushed, which can be recognized. You can detect something like "alt has been pressed in the last half second, ctrl has too, 'a' has been pressed… I guess that's good enough to push ctrl+alt+a!" when it's really not because they might have just pressed alt, released it, then pressed ctrl+a.
Engineer #9
Posted 02 September 2013 - 02:16 AM
Its better then nothing in my opinion
Zudo #10
Posted 02 September 2013 - 02:18 AM
SpoilerSTUPID +1 LIMIT!

I think this would be a good idea! I like the nice simple API calls.
electrodude512 #11
Posted 02 September 2013 - 07:53 PM
GravityScore has a keyboard shortcut coroutine in his LuaIDE. Just use that.
ElvishJerricco #12
Posted 02 September 2013 - 10:21 PM
GravityScore has a keyboard shortcut coroutine in his LuaIDE. Just use that.

As I have explained: It's impossible to do keyboard shortcuts properly in CC short of shortcuts as simple as ctrl+<letter>, which is why I'm making this suggestion.
immibis #13
Posted 03 September 2013 - 07:01 AM
It seems simpler to add 3 flags (or a bitmask) to every key event.