8 Replies - 24531 Views - Last Post: 13 April 2012 - 12:35 PM Rate Topic: -----

#1 kurent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-December 07

Keypress event not working

Posted 27 December 2007 - 09:31 AM

I have a weird problem. If I just start a new windows application project, add a keypress event to the form so it looks like this:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
		MessageBox.Show(e.KeyChar.ToString(), "Your input");
}


and it works. But if I try to do the same with one of my projects nothing happens when a key is pressed. Could something else be interfering with the event? I'm not using any other event handlers.

Is This A Good Question/Topic? 0
  • +

Replies To: Keypress event not working

#2 phatSolutions  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-December 07

Re: Keypress event not working

Posted 27 December 2007 - 09:53 AM

View Postkurent, on 27 Dec, 2007 - 09:31 AM, said:

I have a weird problem. If I just start a new windows application project, add a keypress event to the form so it looks like this:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
		MessageBox.Show(e.KeyChar.ToString(), "Your input");
}


and it works. But if I try to do the same with one of my projects nothing happens when a key is pressed. Could something else be interfering with the event? I'm not using any other event handlers.



in the other forms where this is not working are you wiring the event? Like so..

Form1.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);


Change Form1 to whatever the other form names are.
Was This Post Helpful? 0
  • +
  • -

#3 kurent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-December 07

Re: Keypress event not working

Posted 27 December 2007 - 11:00 AM

Yes, it adds that automatically when you select the form in form designer and double click on the keypress event. That's why the whole thing is so puzzling. I do the exact same thing in my project as I do if I try to start from scratch in a new project.
Was This Post Helpful? 0
  • +
  • -

#4 kurent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-December 07

Re: Keypress event not working

Posted 28 December 2007 - 08:03 AM

Seems this is a focus problem. If I delete all the buttons and text boxes it works.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Keypress event not working

Posted 28 December 2007 - 08:12 AM

You need to set the KeyPreview Property of your Form to True so it will recognize the key press event.

Hops that helps :)
Was This Post Helpful? 2
  • +
  • -

#6 phatSolutions  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-December 07

Re: Keypress event not working

Posted 28 December 2007 - 09:18 AM

View PostPsychoCoder, on 28 Dec, 2007 - 08:12 AM, said:

You need to set the KeyPreview Property of your Form to True so it will recognize the key press event.

Hops that helps :)


Good catch..didn't think of that one.
Was This Post Helpful? 0
  • +
  • -

#7 kurent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-December 07

Re: Keypress event not working

Posted 28 December 2007 - 11:49 AM

Yay now it works! Thank you!

I managed to read a 3D object file, draw it, rotate it...but using keyboard was beyond me haha.
Was This Post Helpful? 0
  • +
  • -

#8 phatSolutions  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-December 07

Re: Keypress event not working

Posted 28 December 2007 - 11:52 AM

View Postkurent, on 28 Dec, 2007 - 11:49 AM, said:

Yay now it works! Thank you!

I managed to read a 3D object file, draw it, rotate it...but using keyboard was beyond me haha.


I hear ya, hate when seemingly easy tasks stump me but the complicated ones seem easy to figure out.

Glad it's working for you.
Was This Post Helpful? 0
  • +
  • -

#9 alfkonee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-June 11

Re: Keypress event not working

Posted 13 April 2012 - 12:35 PM

I also found another way to do this
void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 18)
            {
               //what ever u want do do here.
            }
        }


hope it helps someone
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1