How to create lambda of type ((KeyboardEvent) -> dynamic)?

I want to listen keyboard events. So I set lambda to document.onkeypress , which should be of type:

var onkeypress: ((KeyboardEvent) -> dynamic)?

Currently I’m getting this:

Lambda should return dynamic type, and I do return it, don’t understand what’s wrong with this construction.

First of all, please copy paste the code inside backtiks like this.

I believe what you need is the lambda itself to be a dynamic:

val myLambda: (KeyboardEvent) -> dynamic = { event: KeyboardEvent ->
    // stuff
document.onkeypress = myLambda.asDynamic()

@lamba92 thanks, but your example doesn’t work either:

Oh sorry, the window.onload lambda wants a dynamic as well, i did not noticed:

windows.onload = {
    // stuff
    return Unit.asDynamic()

Yes thanks, it is even working without .asDynamic()
Finall variant:

   window.onload = {
        document.onkeypress = { event: KeyboardEvent ->
            // Do work