Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Techies,

I have some applications which I want to launch when user presses specific keys from the keyboard, and for this I want to use WndProc method.

I am successful in this in a WinForms application. I want to develop a Windows Service to achieve this.
I get the following errors while trying to use the code in a Windows Service.

1. RegHotKeys.CreateParams : no suitable method found to override
2. RegHotKeys.WndProc(ref Message) : no suitable method found to override
3. ServiceBase does not contain a definition for WndProc
4. ServiceBase does not contain a definition for CreateParams

Following is the code snippet:

protected override void WndProc(ref Message keyPressed)
        {
            Keys keyData = ((Keys)((int)((long)keyPressed.WParam))) | Control.ModifierKeys;

            if (keyPressed.LParam.ToInt32() == hotKey1)
            {
               //ACTION 1
            }
            else if (keyPressed.LParam.ToInt32() == hotKey2)
            {
               //ACTION 2
 
            }
            else if (keyPressed.LParam.ToInt32() == hotKey3)
            {
               //ACTION 3
 
            }
            else if (keyPressed.LParam.ToInt32() == hotKey4)
            { 
               //ACTION 4
            }

            base.WndProc(ref keyPressed);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;
                cp.ExStyle |= 0x80;  // Turn on WS_EX_TOOLWINDOW
                return cp;
            }
        }

So can anybody suggest what could be the possible solution to this please?

What I have tried:

I am successful in this in a WinForms application.
Posted
Updated 16-Oct-18 6:48am

You can't.

Services run under a desktop that never receives the input focus so it never gets any notification of keyboard or mouse messages.
 
Share this answer
 
You can't do that.

A Windows service is a non-interactive process. It cannot display any user interface, because it's not part of an interactive session. Therefore, it can't respond to window messages, because it can't have a window to receive them.

Stick with an application which starts when the user logs in. If you want the user to be able to trigger the shortcut even if your application isn't active, you would need to look at using a global keyboard hook:
A Simple C# Global Low Level Keyboard Hook[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900