Click here to Skip to main content
14,544,662 members
Rate this:
Please Sign up or sign in to vote.
See more:
Basically i am trying to make a program that can handle keyboard events, but it keeps spitting out the error.

cannot assign to 'keydown' because it is a 'method group'


The error
Form1.Keypress(object, KeyPressEventsArg)' hides inherited method 'Control.KeyPress'. Use the new Keboard if hiding was intended 


The Two Programs i used were
private void KeyDown(object sender, KeyEventArgs e)
       {
           if(e.KeyCode == Keys.Enter)
           {
               MessageBox.Show("Enter Key Pressed");
           }
       }

       private void KeyUp(object sender, KeyEventArgs e)
       {

       }

       private void KeyPress(object sender, KeyPressEventArgs e)
       {
           if(e.KeyChar == (char)Keys.Enter)
           {
               MessageBox.Show("Enter was pressed");
           }
       }


The other one was
private new void KeyDown(object sender, KeyEventArgs e)
        {
            Input.ChangedState(e.KeyCode, true);
        }

        private new void KeyUp(object sender, KeyEventArgs e)
        {
            Input.ChangedState(e.KeyCode, false);
        }


public static Hashtable KeyBoard = new Hashtable();

public static bool KeyPressed(Keys key)
{
    if (KeyBoard[key] == null)
    {
        return false;
    }
    else
    {
        return (bool)KeyBoard[key];
    }

}
public static void ChangedState(Keys key, bool state)
{
    KeyBoard[key] = state;
}


What I have tried:

I tried using the solutions vs provided but none of them work
Posted
Updated 22-May-20 8:13am
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are plenty of working examples in the documentation:
Control.KeyPress Event (System.Windows.Forms) | Microsoft Docs[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Your methods' names interfere either with existing events, or existing methods in parent classes.
I would suggest to rename your methods OnKeyDown, OnKeyUp; OnKeyPress etc. Or, if your method interferes with an existing method in a parent class, just declare it as override (if said method is overridable, of course).
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100