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 6720645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:14:13+00:00 2026-05-26T09:14:13+00:00

I used following documentation to write a method in Vala to send key press

  • 0

I used following documentation to write a method in Vala to send key press events (e.g. <Ctrl>V) to the application which has the focus.

Here is the code:

public void press(string accelerator)
{
    X.KeyEvent key_event;
    if(create_key_event(accelerator, X.EventType.KeyPress, out key_event)) {
        X.Event event = (X.Event)key_event;
        key_event.display.send_event(key_event.window, false,
            X.EventMask.KeyPressMask, ref event);
    }
}

private bool create_key_event(string accelerator,
    int event_type, out X.KeyEvent key_event)
{
    // convert accelerator
    uint keysym;
    Gdk.ModifierType modifiers;
    Gtk.accelerator_parse(accelerator, out keysym, out modifiers);
    unowned X.Display display = Gdk.x11_get_default_xdisplay();
    key_event = X.KeyEvent();

    int keycode = display.keysym_to_keycode(keysym);

    if(keycode != 0) {
        X.Window root_window = Gdk.x11_get_default_root_xwindow();

        // get window with focus
        X.Window focus;
        int revert_to_return;
        display.get_input_focus(out focus, out revert_to_return);

        key_event.display = display;
        key_event.root = root_window;
        key_event.window = focus;
        key_event.subwindow = X.None;
        key_event.time = X.CURRENT_TIME;
        key_event.keycode = keycode;
        key_event.state = modifiers;
        key_event.type = event_type;
        key_event.x = 1;
        key_event.y = 1;
        key_event.x_root = 1;
        key_event.y_root = 1;
        return true;
    }

    return false;
}

This just works fine on gtk2 applications. However gtk3 applications seem to ignore such events altogether.
Is there a way to send such events to gtk3 applications as well?

  • 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-05-26T09:14:13+00:00Added an answer on May 26, 2026 at 9:14 am

    I have done some more investigation on this. It seems that such events will be received by gtk3 application when sent with the XTest extension also mention on the stated documentation.

    Here is a sample in vala and you will need to bind it against xtst.vapi to be able to build it:

    public void press(string accelerator)
    {
      if(perform_key_event(accelerator, true)) {
        debug("Successfully pressed key " + accelerator);
      }
    }
    
    private bool perform_key_event(string accelerator, bool press)
    {
        // convert accelerator
        uint keysym;
        Gdk.ModifierType modifiers;
        Gtk.accelerator_parse(accelerator, out keysym, out modifiers);
        unowned X.Display display = Gdk.x11_get_default_xdisplay();
        int keycode = display.keysym_to_keycode(keysym);
    
        if(keycode != 0) {
    
          if(Gdk.ModifierType.CONTROL_MASK in modifiers) {
            int modcode = display.keysym_to_keycode(Gdk.Key.Control_L);
            XTest.fake_key_event(display, modcode, press, delay);  
          }
          if(Gdk.ModifierType.SHIFT_MASK in modifiers) {
            int modcode = display.keysym_to_keycode(Gdk.Key.Shift_L);
            XTest.fake_key_event(display, modcode, press, delay);
          }
    
          X.Test.fake_key_event(display, keycode, press, 0);                
          return true;
        }
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Per the MSDN documentation , the following syntax is used: // A read-write instance
I read about documentation of android and used following code to send a notification
The following algorithm is used with Google Maps: http://code.google.com/intl/ru-RU/apis/maps/documentation/utilities/polylinealgorithm.html Are there any alternatives? The
I used following code, but it displays only 2 digit of ISO country name.
i have used following code to repeat a process creation/close iteratively dim vProcessInfo as
I have used following code to create a simple PDF file. It executes fine
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
sending mail along with embedded image using asp.net I have already used following but
I used the following query to find duplicates: SELECT userID, COUNT(userID) AS NumOccurrences FROM
I used the following piece of code in the service to debug the service

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.