Jump to content




Custom Events

lua help api

  • You cannot reply to this topic
11 replies to this topic

#1 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 09 September 2015 - 01: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 by DannySMc, 09 September 2015 - 01:24 PM.


#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 09 September 2015 - 01: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.

#3 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 09 September 2015 - 01:45 PM

View PostMKlegoman357, on 09 September 2015 - 01:35 PM, said:

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?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 September 2015 - 01: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?

#5 HDeffo

  • Members
  • 214 posts

Posted 09 September 2015 - 01: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

#6 Exerro

  • Members
  • 801 posts

Posted 09 September 2015 - 03: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.

#7 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 09 September 2015 - 04:35 PM

CTRL+S for saving?
Warning it may shut down your computer XD
APIs and Utilities subforum -> keyboard api

Edited by Konlab, 09 September 2015 - 04:37 PM.


#8 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 09 September 2015 - 04: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?

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 09 September 2015 - 04: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.

#10 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 09 September 2015 - 09:30 PM

View PostLyqyd, on 09 September 2015 - 04:58 PM, said:

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?

#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 September 2015 - 11:23 PM

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.

#12 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 09 September 2015 - 11:43 PM

View PostBomb Bloke, on 09 September 2015 - 11:23 PM, said:

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!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users