views:

258

answers:

3

Hi, I'm working on a online quiz client where we use a dedicated custom-made linux distro which contains only the quiz client software along with text editors and other utility software. When the user has started the quiz, I want to prevent him/her from minimizing the window/closing it/switching to the desktop or other windows. The quizzes can be attempted using only the mouse, so I need the keyboard to be completed disabled for the period of the quiz. How could I do this, using Qt or Mono? I'm ready to use any low-level libraries/drivers, if required. Thanks Evans

+1  A: 

Hey,

Did you try to use EventFilter ? You have the opportunity to block all the events related to, as instance, keypress...

More information here : http://qt.nokia.com/doc/4.6/eventsandfilters.html

Hope it helps !

Something like :

bool MyWidget::event(QEvent *event)
{
    if (event->type() == QEvent::KeyPress) 
    {
        return true;
    }
    return QWidget::event(event);
}
Andy M
Can I use this to block "global" key presses like alt+tab, control+alt+del? I don't think so.
Evans
Edit : I tried and you're right, It's not working with multiple key presses... I don't get why... There should be something you can do to "walk around" that problem... You could also have a look at examples of KeyLoggers...
Andy M
+4  A: 

You may use QWidget::grabKeyboard and QWidget::grabMouse, and please note the warning in comments:

Warning: Bugs in mouse-grabbing applications very often lock the terminal. Use this function with extreme caution, and consider using the -nograb command line option while debugging.

Mason Chang
It is very annoying when an application that has a grab hangs or becomes unresponsive for longer periods of time, so use with care. If you ever get into this kind of situation, your only rescue is to press sysrq+alt+r (to switch to basic kernel keyboard handler) and then alt+f1 (to switch to text console), then kill your app. You need to turn on "magic sysrq" key by ` echo 1 > /proc/sys/kernel/sysrq`.
liori
So, I can use QWidget::grabKeyboard to totally block all keyboard input, even special ones like Alt+Tab, Control+Alt+F1, Control+Alt+Del etc? Plus, will it work in GNOME? Is it possible to call this function directly from Mono?
Evans
I didn't try this one GNONE nor Mono. Could you have a try? and please be sure to add an "EXIT" button on your testing widget.
Mason Chang
Hi, I created a simple Qt Application to test this. I used window.grabKeyboard(). There was not effect - I tested it on GNOME as well as in KDE. Any suggestions? Any sequence of calls I must use to get it to work?
Evans
A: 

Have you looked at XGrabKeyboard? That should do a global grab of the keyboard.

Chris
Thanks. It worked!
Evans
I was not able to block Control+Alt+F1 using this. Is there any way I can do that?
Evans
It must be handled at a higher level. I don't have any experience with it, but I would start here:http://www.charvolant.org/~doug/xkb/html/index.html
Chris