1

I have this code, which does not want to work! The point is to make a key viewer which shows the pressed cursor keys!

However, none of the keyUpPress method works.

bool debug = true;
void keyUpPress(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Up)
   {
       if (debug == true)
       {
           MessageBox.Show("CURSOR UP", "Cursor!");
       }
       pictureBox1.Image = Properties.Resources.key_up_pressed;
       pictureBox1.Refresh();
   }
  • Please explain what do you mean with _none of the keyUpPress method works_ Do you have an error message? Is the code called? What event is binded to this event handler? – Steve Aug 14 '16 at 20:56
  • @Steve The code compiles perfectly (no errors or warnings), however when I press the cursor up key, NOTHING happens. – Distant Graphics Aug 14 '16 at 20:57
  • 4
    Do you have binded the event handler in your form or textbox event KeyPress? – Steve Aug 14 '16 at 20:58
  • I did not bind any eventHandlers to the picturebox, but I don't think I can do that either. – Distant Graphics Aug 14 '16 at 20:59
  • Yes the PictureBox is not a control that receives the input keys. You can resolve your problems using the form as receiver of your input key, but this could be the source of other problems if you have also controls like textboxes or buttons together. In alternative, [look at this answer](http://stackoverflow.com/questions/3562235/panel-not-getting-focus/3562449#3562449) where a Panel control (like a PictureBox it cannot handle key events or focus) is subclassed and transformed in a focusable control with all the keystroke handled – Steve Aug 14 '16 at 21:13

1 Answers1

1

keyUpPress is an event that triggers after lifting a key (after holding it down).

You can't bind a eventHandler to a PictureBox, but you can bind it to the form (if it's a forms application). Example code (with Form1 as the name of the form):

    private void Form1_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            if (debug == true)
            {
                MessageBox.Show("CURSOR UP", "Cursor!");
            }
            pictureBox1.Image = Properties.Resources.key_up_pressed;
            pictureBox1.Refresh();
        }
    }
Daniel
  • 10,641
  • 12
  • 47
  • 85
  • Thanks for the rather quick answer; I suspect you are correct. However, I do not currently have the time to test out your code; I will determine whether your comment answered my question tomorrow. – Distant Graphics Aug 14 '16 at 21:07