Unicode keyboard

I am trying to create a unicode keyboard. The “keyboard” is a bunch of buttons in a popup window. Here is a demonstration version of what I have so far:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject

uni_chars = 'à á â ã ä å ă ǎ ā '.split()

class PopupWindow(Gtk.Window):
    @GObject.Signal
    def clicked(self, letter: str):
        pass

    def __init__(self):
        super().__init__()
        self.set_title('Popup')

        box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
        for label in uni_chars:
            button = Gtk.Button.new_with_label(label)
            button.set_relief(Gtk.ReliefStyle.NONE)
            button.set_can_focus(False)
            button.connect('clicked', self.on_button_clicked)
            box.add(button)
        self.add(box)
        self.show_all()

    def on_button_clicked(self, button):
        self.emit('clicked', button.get_label())

class MainWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        self.set_title('Main')
        self.connect('destroy', Gtk.main_quit)

        box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)

        button = Gtk.Button.new_with_label('Popup Window')
        button.connect('clicked', self.on_button_clicked)
        box.add(button)

        entry = Gtk.Entry.new()
        entry.grab_focus()
        box.add(entry)

        self.add(box)
        self.show_all()

    def on_button_clicked(self, button):
        self.popup_window = PopupWindow()
        self.popup_window.connect('clicked', self.on_char_button_clicked)

    def on_char_button_clicked(self, button, label):
        # Send a key press event to type the character at the cursor.
        current_event = Gtk.get_current_event()
        device = current_event.get_device()
        window = current_event.get_window()

        event = Gdk.Event.new(Gdk.EventType.KEY_PRESS)
        event.keyval = Gdk.unicode_to_keyval(ord(label))
        event.set_device(device)
        event.window = window
        event.put()

if __name__ == '__main__':
    win = MainWindow()
    Gtk.main()

I would like for the unicode character associated with a button to appear in the entry when I click the button.

I originally sent the event from the popup window. I changed to the current scheme thinking that it might help to have the event originate in the same widget hierarchy as the target. However, it is still the case that no character appears in the entry.

Am I even close? Is there a better approach?

Getting the window from the event yields the window of PopupWindow. I get the desired result when I use the window of MainWindow instead. Case closed.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.