5120bee
5120bee

Reputation: 709

I can't get ProcessCmdKey to work?

My code:

 using System.Windows.Forms

 class Class1
 {
      protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
      {
           if (keyData == Keys.Up)
           {
                Console.WriteLine("You pressed the Up arrow key");
                return true;
           }

           ...//and other code lines for Keys.Down, Keys.Left, Keys.Right

           return true;
      }
 }

But all I get the error:

(namespace).(class).ProcessCmdKey(ref System.Windows.Forms.Message, System.Windows.Forms.Keys): no suitable method found to override.

And if I switch out the

 return true;

for

 return base.ProcessCmdKey (ref msg, keyData);

I get the error:

'object' does not contain a definition for ProcessCmdKey.

In fact, my ProcessCmdKey text isn't even turning green in color but it stays black when I know it's supposed to be a green color.

Is it something with the .NET Framework version I'm using? If anything, I'm using Microsoft Visual Studio 2013 to compile and run my codes.

Or is it something to do with the security level of my class? I'm working with a public-level class

I'm just learning how to register user key inputs as a beginner. I've looked ALL OVER the internet about this topic and this is the same code that's being brought up but I can't get it to work. Any help is welcome.

Upvotes: 4

Views: 3390

Answers (1)

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

The method ProcessCmdKey is defined in the class System.Windows.Forms.Control. So unless you are overriding it in a control (or form) this won't work.

Your class Class1 does not inherit from a control, therefore you can't overrride the method (because it doesn't exist).

The Error message

'object' does not contain a definition for ProcessCmdKey

Pretty much tells you exactly that. object is the base class for your Class1 (implicit) and the class object (baseclass of everything) doesn't have that method.

Form.ProcessCmdKey on MSDN

Control.ProcessCmdKey on MSND

Upvotes: 1

Related Questions