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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:39:26+00:00 2026-05-23T03:39:26+00:00

I’m using a C# app to listen for a global combination of keys (ctrl+F9)

  • 0

I’m using a C# app to listen for a global combination of keys (ctrl+F9) that will bring a specific window to front.

This is the code I’m using to bring a window to front, it works only if fired by a Button event:

    private void button3_Click(object sender, EventArgs e)
    {
        SetForegroundWindow(ptrActiveWindow.ToInt32());
        ShowWindowAsync(ptrActiveWindow, SW_RESTORE);
    }

For hooking, I’ve used a class taken from http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx, which is fully listed here:

http://code.google.com/p/code-commenter/source/browse/trunk/key+preview/globalKeyboardHook.cs?spec=svn3&r=3

Whenever I have a F9 KeyUp event after a CTRL KeyDown and before a CTRL KeyUp (CTRL is still pressed), I call my method:

    private void restore(IntPtr hWnd)
    {

      IntPtr ptrCurrentActiveWindow = GetForegroundWindow(); //comment line
      ShowWindowAsync(ptrCurrentActiveWindow, SW_MINIMIZE); //comment line

        ShowWindowAsync(hWnd, SW_RESTORE);
        SetFocus(hWnd);
        SetForegroundWindow(hWnd.ToInt32());

    }

This does nothing. My window is activated in the background (I can see it blinking in the taskbar), but does not get restored.

The only way I can move around this is to use the commented code: minimize the currently active window and afterwards restore the window I want to see.

All help is appreciated,
Thanks.

Global hotkey, working version:

    private void Form1_Load(object sender, EventArgs e)
    {
        string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName;
        short HotkeyID = GlobalAddAtom(atomName);
        if (!RegisterHotKey(this.Handle, HotkeyID, (uint)GlobalHotkeys.MOD_CONTROL, (uint)Keys.D5))
            listBox.Items.Add("failed: " + "unable to register hotkey. Error: " + Marshal.GetLastWin32Error().ToString());
        else
            listBox.Items.Add("succeeded adding hotkey id"+(uint)Keys.D5);
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_HOTKEY = 0x0312;

        if (m.Msg == WM_HOTKEY)
        {
            if ((short)m.WParam==HotkeyID)
                listBox.Items.Add("Hotkey."+ (short)m.WParam);
        }
        base.WndProc(ref m);
    }
  • 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-23T03:39:27+00:00Added an answer on May 23, 2026 at 3:39 am

    Your problem is that you didn’t register a global hotkey but used a keyboard hook instead. Keyboard hooks are not designed to be used as global hotkeys.

    Use the RegisterHotKey function instead.
    Check this example: http://www.pinvoke.net/default.aspx/user32.registerhotkey

    Your immediate problem is that an application can’t put itself into the foreground whenever it likes. Because that’s annoying to the user. It can only do that during specific event. Like application startup or when handling a real global hotkey.

    The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

    • The process is the foreground process.
    • The process was started by the foreground process.
    • The process received the last input event.
    • There is no foreground process.
    • The foreground process is being debugged.
    • The foreground is not locked (see LockSetForegroundWindow).
    • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
    • No menus are active.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.