Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate/spoof keypress event in Haskell?

How to emulate/spoof keypress event in Haskell?

I have an onscreen number keypad in my Gui which I want to use to write into a number of text entrys in the window. The Keypad is made up of button widgets and what I want to do is have the onButtonClicked handler send a keyPressEvent so that the appropriate char/number is written into the text entry that has focus. I started by looking at Graphics.UI.Gtk.Gdk.Events and made an keypress event

Event my1Event = Key True True Graphics.UI.Gtk.Gdk.Events.currentTime [] False False False 0xffb1 "KP_1" Nothing

I couldn't figure out how to dispatch this event. Anyway it turns out Event is deprecated and should use EventM. I found widgetEvent :: WidgetClass self => self -> EventM t Bool in Graphics.UI.Gtk.Abstract.Widget. In the api it says:

Rarely-used function. This function is used to emit the event signals on a widget (those signals should never be emitted without using this function to do so). If you want to synthesize an event though, don't use this function; instead, use mainDoEvent so the event will behave as if it were in the event queue.

So I looked at mainDoEvent :: EventM t () in Graphics.UI.Gtk.General.General but am unsure as how to use EventM to create a keypress and couldn't find an example of mainDoEvent in use. Can anybody advise me how to create a keypress event using EventM and pass it to mainDoEvent or some other way to do this? Thanks.

like image 262
Joseph Kennedy Avatar asked Nov 14 '22 20:11

Joseph Kennedy


1 Answers

Generating an event is the Wrong Way. Instead, you should insert your text using the appropriate function for your text widget. For example, for TextBuffers, use textBufferInsertAtCursor.

like image 188
Daniel Wagner Avatar answered Dec 19 '22 15:12

Daniel Wagner