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

Custom Events

Started by DannySMc, 09 September 2015 - 12:17 PM
DannySMc #1
Posted 09 September 2015 - 02:17 PM
I have a simple question but:

Say I wanted to make a new event so for example I wanted to set a event for holding CTRL and then a button press, how would I register that as an event? Like for example when I click CTRL and press S it will come back as:

Event: "control_and_key"
Return 1: "S"

How would I register that to work with os.pullEvent()?

So sorry, what would is the code for it? Example would be amazing even if it is just say pressing F1 and changing the event name to function_key, thanks
Edited on 09 September 2015 - 12:24 PM
MKlegoman357 #2
Posted 09 September 2015 - 02:35 PM
To queue a custom event you'd use os.queueEvent(). If you're using this system in your own program, you could run another function (with parallel API or a coroutine manager) to constantly be catching events and dealing with them. Or you could simply incorporate this logic into your current event loop, if your program has one.
DannySMc #3
Posted 09 September 2015 - 02:45 PM
To queue a custom event you'd use os.queueEvent(). If you're using this system in your own program, you could run another function (with parallel API or a coroutine manager) to constantly be catching events and dealing with them. Or you could simply incorporate this logic into your current event loop, if your program has one.

Well the coroutine manager you helped me with I'm planning to add more events to it for just ease of access, so like holding CTRL + T starts a terminate event, I want to do the same thing so CTRL and S will save something etc? So I need a way of just allowing it to constantly check? Like when I press a key? So I can set a function at the top or in a API and that will like register it? I'm not sure?
Bomb Bloke #4
Posted 09 September 2015 - 02:52 PM
Well, let's say your coroutine manager is doing the usual business of pulling events, checking whether they match the filters of your coroutines, and resuming accordingly - you'd simply have it translate the events before resuming coroutines with them, yeah?
HDeffo #5
Posted 09 September 2015 - 02:53 PM
The best way to do this is quickly check button presses and if ctrl + key were pressed really close to each other trigger the even os.queueEvebt. Last I checked there's no perfect way to detect if both are down at the same time.


Also you probably already know but holding ctrl + S shuts the computer down…so your save button should probably be something else
Exerro #6
Posted 09 September 2015 - 04:27 PM
You know there's key_up events now right? You can quite easily make a key-down tracker that keeps a track of what keys are being held. If you do that, it's just 1 line of code to tell if you're holding ctrl so you can fire a custom ctrl_key event.
Konlab #7
Posted 09 September 2015 - 05:35 PM
CTRL+S for saving?
Warning it may shut down your computer XD
APIs and Utilities subforum -> keyboard api
Edited on 09 September 2015 - 03:37 PM
DannySMc #8
Posted 09 September 2015 - 05:52 PM
If anyone doesn't understand what I am trying to say,

How do I make the code to run in the background to check for this event all the time, then when the event fires it will queue the event, so for example, so I want to wait for CTRL and O to be pressed together then I write programs normally because the custom event code happens in the background…? So I can just do os.pullEvent("open") or whatever I want to do:

Example:


function newevent()
  -- do the code to get the CTRL and O press
  os.queueEvent("Open")
end

-- Register the newevent as a normal event, like key, timer, terminate etc etc

-- run normal code:
local function test()
  -- draw a screen
  while true do
    local arg = os.pullEvent()
    if arg == "open" then
      -- do open code here
    end
  end
end

test()

So I want to make an event that is registered LIKE key events, terminate, paste, char, mouse_click etc etc? Is this possible?
Lyqyd #9
Posted 09 September 2015 - 05:58 PM
There's no "registration" for events. You'd need to run a program that watches other events and queues events when appropriate. Maybe look at GopherAtl's ctrlkeys program.
DannySMc #10
Posted 09 September 2015 - 10:30 PM
There's no "registration" for events. You'd need to run a program that watches other events and queues events when appropriate. Maybe look at GopherAtl's ctrlkeys program.

So is it not really do-able? if it helps I run it in a coroutine manager?
Bomb Bloke #11
Posted 10 September 2015 - 12:23 AM
It's not "do-able" using the structure in your code snippet, at least.

As mentioned, you can have your manager translate events before passing them to its child coroutines.

You could also override os.pullEvent() and have that perform the translations.

And there's furthermore the option of rigging up something like rednet.run(), which translates certain modem_message events into rednet_message events.

But personally I'd recommend just build the functionality straight into your coroutine manager. That's the cleanest solution.
DannySMc #12
Posted 10 September 2015 - 12:43 AM
It's not "do-able" using the structure in your code snippet, at least.

As mentioned, you can have your manager translate events before passing them to its child coroutines.

You could also override os.pullEvent() and have that perform the translations.

And there's furthermore the option of rigging up something like rednet.run(), which translates certain modem_message events into rednet_message events.

But personally I'd recommend just build the functionality straight into your coroutine manager. That's the cleanest solution.

Ahh fair enough, bit of a pain but sweet, thanks guys!