possibility to have two same installeventhandlers at same time?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

possibility to have two same installeventhandlers at same time?

Post by Bugala »

I am not needing this now, but I can see there can come a situation where I could need this.

What I was hoping to do was following:

Code: Select all

InstallEventHandler({onkeydown = Function() DebugPrint("test") EndFunction})
InstallEventHandler({onkeydown = Function() DebugPrint("test2") EndFunction})
I was hoping result to be that when I push a key down, it would have debugprinted both "test" and "test2", however, seems there can be only one "onekeydown" event at a time, and hence only "test2" was debugprinted.

Is there any other way to achieve this, except by coding it myself in way of that I refer onkeydown to go to a function which takes care that all my "onkeydown" events are being executed?

I mean, I could do something like:

Code: Select all

InstallEventHandler({onkeydown = onkeydownfuncs

function onkeydownfuncs()
foreach(onkeydownfuncslist, function (id, func)
						func()
					endfunction
to execute more than just one happening. But is there any Hollywood supported way?

For example it could be handy if each InstalEventHandler would return an ID, and then I could later on use to remove those InstallEventHandlers by using that ID, this way I could use for example:

Code: Select all

ID1 = InstallEventHandler({onkeydown = Function() DebugPrint("test") EndFunction})
ID2 = InstallEventHandler({onkeydown = Function() DebugPrint("test2") EndFunction})
to get both "test" and "test2" to be printed, and then at later point use:

Code: Select all

RemoveEventHandler(ID2)
and now it would debugprint only "test" anymore when pressing a key down.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: possibility to have two same installeventhandlers at same time?

Post by SamuraiCrow »

Bugala wrote: Tue Feb 23, 2021 11:26 am Is there any other way to achieve this, except by coding it myself in way of that I refer onkeydown to go to a function which takes care that all my "onkeydown" events are being executed?

I mean, I could do something like:

Code: Select all

InstallEventHandler({onkeydown = onkeydownfuncs

function onkeydownfuncs()
foreach(onkeydownfuncslist, function (id, func)
						func()
					endfunction
to execute more than just one happening. But is there any Hollywood supported way?

For example it could be handy if each InstalEventHandler would return an ID, and then I could later on use to remove those InstallEventHandlers by using that ID, this way I could use for example:

Code: Select all

ID1 = InstallEventHandler({onkeydown = Function() DebugPrint("test") EndFunction})
ID2 = InstallEventHandler({onkeydown = Function() DebugPrint("test2") EndFunction})
to get both "test" and "test2" to be printed, and then at later point use:

Code: Select all

RemoveEventHandler(ID2)
and now it would debugprint only "test" anymore when pressing a key down.
Rolling your own is the correct way with either a bitset to keep track of which handlers are active or a table solution with a 2 table reverse lookup to which function goes to which index key in the first table. If you need more than 32 handlers, the bitset will get needlessly tricky. Actually adding a "last" entry variable either in the table or parallel to it will be sufficient if order is insignificant but you'll still need a "slot" number to be maintained independently. I'll see if I can do an example later but Hollywood does only one event handler per event type AFAIK.
I'm on registered MorphOS using FlowStudio.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: possibility to have two same installeventhandlers at same time?

Post by SamuraiCrow »

After some thought, the 2 table method is essentially a hash-set implementation. Here it is:

Code: Select all

global HandleKey={}
HandleKey["functions"]={}
HandleKey["set"]={}
HandleKey["last"]=0

Function HandleKey:InstallHandler(func)
   Local current=self.last
   self.last=self.last+1
   self.functions[current]=func
   self.set[current]=current
   Return (current)
EndFunction

Function HandleKey:RemoveHandler(num)
   self.last=self.last-1
   Local current=self.last
   If num<>set[current]
      self.functions[num]=self.functions[self.set[current]]
      set[num]=set[current]
   EndIf
   self.functions[current]=Nil
   self.set[current]=Nil
EndFunction

Function HandleKey:process(id)
   For func In IPairs(self.functions) Do func(id)
EndFunction

InstallEventHandler({onKeyDown=HandleKey:process})
That's the hash-set version. The bit-set version is a little fancier but not by much.
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: possibility to have two same installeventhandlers at same time?

Post by Bugala »

quite compact solution. Thanks. Might use it if need arises, I can see there could be need in future.
Post Reply