User Control and the key press event

C

Clive Foley

Hey,
i'm trying to capture key press event on my user control
but it won't work for me. If messed around with the focus
event s to see what is happening. Every time a key board
key is pressed my control loses focus.Please reply to
(e-mail address removed) if you can.

Any ideas?
 
H

Herfried K. Wagner [MVP]

* "Clive Foley said:
i'm trying to capture key press event on my user control
but it won't work for me. If messed around with the focus
event s to see what is happening. Every time a key board
key is pressed my control loses focus.Please reply to
(e-mail address removed) if you can.

Post your code.
 
C

Clive Foley

private void KeyPressed(object o, KeyEventArgs e)
{
MessageBox.Show("key press") ;
switch(e.KeyData)
{
case Keys.Left :
myPaddle.MovePaddleLeft() ;
break ;
case Keys.Right :
myPaddle.MovePaddleRight() ;
break;
case Keys.Escape :
GameOver() ;
break ;
default :
break ;
}
}
this is in the constructor:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyPressed) ;

also i have found that if i query the CanFocus property i find that it
returns false indicating my control cant receive focus.

Any ideas on how to fix this?
 
H

Herfried K. Wagner [MVP]

* Clive Foley said:
also i have found that if i query the CanFocus property i find that it
returns false indicating my control cant receive focus.

Mhm...

Try to override some of the 'Process*' methods defined in the
usercontrol class. Maybe you'll get the notification about keypress
there. You can try to set the 'ControlStyles.Selectable' control style
too.
 
C

Clive Foley

Ok,
overloading the process method captures the keyboard input.All i have to
work with is a reference to a Message struct.How do i use this struct to
figure out what key was pressed and then raise the keypressed event with
KeyeventArgs which i now have to define.I don't quite understand what
the Message struct does. Its Msg property returns an int but what does
this stand for?

Thanks Herfried you have been very helpful so far
 
H

Herfried K. Wagner [MVP]

* Clive Foley said:
Ok,
overloading the process method captures the keyboard input.All i have to
work with is a reference to a Message struct.How do i use this struct to
figure out what key was pressed and then raise the keypressed event with
KeyeventArgs which i now have to define.I don't quite understand what
the Message struct does. Its Msg property returns an int but what does
this stand for?

Add this to your usercontrol:

\\\
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Right Then
MsgBox("Right key Pressed!")
End If
MyBase.ProcessCmdKey(msg, keyData)
End Function
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top