What are these functions called and how to implement they?

frame frame86 at live.com
Thu Jan 28 18:27:09 UTC 2021


On Thursday, 28 January 2021 at 17:10:57 UTC, dog2002 wrote:
> I saw these functions in some projects. For example: in Dagon 
> (https://gecko0307.github.io/dagon/) there are functions like 
> onKeyDown. This function doesn't need to call - it checks 
> pressed keys every time. Or Update in Unity (game engine). It 
> doesn't need to call, but it executes itself every frame.
>
> Okay, maybe it sounds confusing. I just want to understand how 
> to implement functions that wait for events and execute code 
> inside the function. Something like this:
>
> void onKeyPressed(int key) {
>     //the code to be executed if a key has been pressed
> }

Not 100% sure what you mean but I guess you ask how to implement 
a handler? If an event occurs, a routine decides to call your 
onKeyPressed function and pass the keyCode which was pressed. The 
routine must be registered on a event source that will emit the 
events. The routine is basically just a callback and that is 
indeed called every frame again if an event occurs.


More information about the Digitalmars-d-learn mailing list