Virtual keyboard for the Pi with auto-hide functionality
Asked Answered
D

1

7

I am using PyGObject to create an UI that will be run on a 7" official RPi touchscreen connected to a Pi 3 running Raspbian. As part of this interface, the UI will need an on-screen keyboard. I am aware of two virtual keyboard programs for the Pi: Matchbox Keyboard and Florence.

The problem is that I want to imitate the behavior of a smartphone keyboard as much as possible, but don't know how to do so. What I want to do is similar to this, except I want the keyboard to auto-hide and to be on top of the main window. How can this be done?

EDIT: I've tried both of these programs and haven't been able to figure out how to achieve this. I can't find an auto-popup option in Matchbox Keyboard, and some people report that it has this capability (here), others say no (here). I assume that some Linux desktop managers support this feature, but not LXDE on the Pi.

Florence seems promising because it has an auto-hide option that sounds like it would do what I want, but when I selected it didn't seem to work.

Dulcle answered 12/3, 2016 at 3:52 Comment(0)
D
3

I finally figured out how to add auto-hide behavior to Matchbox Keyboard. First I read about the --daemon command line argument here which sounded like it would work, but when I tried it the auto-hide feature worked for only some, not all, text entries.

The same README file says:

You can embed matchbox-keyboard into other applications with toolkits that support the XEMBED protocol ( GTK2 for example ).

See examples/matchbox-keyboard-gtk-embed.c for how its done.

I knew about this before but didn't think it would work with PyGObject, until I found out that it does. Adding these lines to my code worked:

p = subprocess.Popen(["matchbox-keyboard", "--xid"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
keyboard = Gtk.Socket()
window.add(keyboard)
keyboard.add_id(int(p.stdout.readline()))

I also created a simple subclass of Gtk.Entry which auto-hides the keyboard when the text entry gains or loses focus:

class TextEntry(Gtk.Entry):
    def __init__(self, window):
        Gtk.Entry.__init__(self)
        self.keyboard = window.keyboard

        self.connect("focus-in-event", self.on_focus_in)
        self.connect("focus-out-event", self.on_focus_out)

    def on_focus_in(self, event, data):
        self.keyboard.show()

    def on_focus_out(self, event, data):
        self.keyboard.hide()
Dulcle answered 28/3, 2016 at 15:26 Comment(5)
Did you noticed that the CPU usage goes at 100% (pcmanfm has problems with matchbox)?Edrisedrock
@Edrisedrock No, presumably because I'm not using it with pcmanfm. I only have it embedded in my GTK app. The problem with pcmanfm is a known issue though: bugs.launchpad.net/raspbian/+bug/1426295Dulcle
You mean that you disabled pcmanfm or that you're not using raspbian?Edrisedrock
I didn't realize until now that the problem occurs even when using matchbox in another app, but the bug report says that it only happens when pcmanfm is run in desktop mode, not when it's launched in a window. I only launch it in a window, so that's probably why I'm not seeing the problem. I'm using the standard Raspbian image on my Pi, the latest PIXEL version.Dulcle
Great! Using --xid parameter prevented the creation of a window (which apparenty creates the problem with pcmanfm). Thank you for the help!Edrisedrock

© 2022 - 2024 — McMap. All rights reserved.