Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:04:52+00:00 2026-06-13T14:04:52+00:00

I need to check whether the Escape key has been pressed during execution of

  • 0

I need to check whether the Escape key has been pressed during execution of some non-GUI code. (The code is in Python, but can easily call into C if necessary.) The code received a function from the GUI that it occasionally calls to check whether it has been interrupted. The question is how to implement this check.

By looking at the documentation, gdk_event_peek seems like an excellent choice for this:

def _check_esc(self):
    event = gtk.gdk.event_peek()
    if event is None or event.type not in (gtk.gdk.KEY_PRESS, gtk.gdk.KEY_RELEASE):
        return False
    return gtk.gdk.keyval_name(event.keyval) == 'Escape'

This doesn’t work, however: the event returned from gtk.gdk.event_peek() is always None when the main loop is not running. Changing it to gtk.gdk.display_get_default().peek_event() doesn’t help either. I assume the events are in the X event queue and are not yet moved to the GDK event queue. The documentation says:

Note that this function will not get more events from the windowing
system. It only checks the events that have already been moved to the
GDK event queue.

So, how does one transfer the event to the GDK event queue or? In other words, when does gtk.gdk.peek_event() ever return an event? Calling gtk.events_pending() doesn’t have any effect.

Here is a minimal program to test it:

import gtk, gobject
import time

def code(check):
    while 1:
        time.sleep(.1)
        if check():
            print 'interrupted'
            return

def _check_esc():
    event = gtk.gdk.event_peek()
    print 'event:', event
    if event is None or event.type not in (gtk.gdk.KEY_PRESS, gtk.gdk.KEY_RELEASE):
        return False
    return gtk.gdk.keyval_name(event.keyval) == 'Escape'

def runner():
    code(_check_esc)
    gtk.main_quit()

w = gtk.Window()
w.show()
gobject.idle_add(runner)
gtk.main()

When running the code, the event printed is always None, even if you press Escape or move the mouse.

I also considered installing a handler for Escape and having the checker process events with the while gtk.events_pending(): gtk.main_iteration() idiom. This results in unqueuing and dispatch of all pending events, including keyboard and mouse events. The effect is that the GUI is responsive enabled while the code runs, which doesn’t look well and can severely interfere with the execution of the code. The only event processed during execution should be the escape key to interrupt it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:04:52+00:00Added an answer on June 13, 2026 at 2:04 pm

    I came up with a runner implementation that satisfies the criteria put forward in the question:

    def runner():
        # _check_esc searches for Escape in our queue
        def _check_esc():
            oldpos = len(queue)
            while gtk.events_pending():
                gtk.main_iteration()
            new = itertools.islice(queue, oldpos, None)
            return any(event.type == gtk.gdk.KEY_PRESS \
                           and gtk.gdk.keyval_name(event.keyval) == 'Escape'
                       for event in new)
    
        queue = []
        # temporarily set the global event handler to queue
        # the events
        gtk.gdk.event_handler_set(queue.append)
        try:
            code(_check_esc)
        finally:
            # restore the handler and replay the events
            handler = gtk.main_do_event
            gtk.gdk.event_handler_set(gtk.main_do_event)
            for event in queue:
                handler(event)
        gtk.main_quit()
    

    Compared to a peek-based solution, its advantage is that it handles the case when another event arrives after the keypress. The disadvantage is that it requires fiddling with the global event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code where I need to check whether all group sizes for
I need to check whether device has been connected properly to My-Wifi network or
I need to check whether a string contains any swear words. Following some advice
I need to check whether a key of datatype set is present or not.
I need to check whether given binary file has write access or not. File
I have some text in a string, I need to check whether that particular
I need to check whether the logged in user is a administrator or non-admin
Often in my code I need to check whether the state of x amount
I have some created web forms and I need to check whether the user
I have the following code where I need to check whether the attribute in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.