How to capture Keypress event in a custom control?

mhsueh001

Freshman
Joined
Dec 31, 1969
Messages
41
I'm trying to create my own form control.

Went through all the File->New->Project, selected Windows Control Library.

I want to capture if a function key is depressed and I tried both the KeyDown event and the Key Press event.

Code:
    Private Sub UserControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
'My Code here
End Sub
    Private Sub UserControl1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'My Code here
    End Sub

However when run the control in a form, neither event is being captured.

What am I doing wrong?

I've tried putting break points at the top of the two delegates above, but the code never enters and runs.

Help!
 
Try making the routines 'Overrides' or try calling Addhandler..... for these events in the form load .
 
donnacha said:
Try making the routines 'Overrides' or try calling Addhandler..... for these events in the form load .

So basically you're saying I need to do an Addhandler and add the keydown event to every control on my custom control? This appears to work when the control is in focus, but I need to catch this event even when the control is not in focus. Any other ideas?
 
Iceplug said:
Make sure that your control can receive the focus, otherwise, those events will go to the form or to another control.

How do I code it so that my control will capture the keydown event even if the control is not in focus. I'm developing a control that needs to scroll down when a key is hit even if the user doesn't have my control in focus.
 
Your control needs to have focus. Why would you want to hog all of the keystrokes?

If your control was placed on the same form as a textbox, then the control who has focus should receive the keystrokes and nothing else.
All of the textboxes in active windows doesn't scroll down when the user hits the down button.
 
Iceplug said:
Your control needs to have focus. Why would you want to hog all of the keystrokes?

If your control was placed on the same form as a textbox, then the control who has focus should receive the keystrokes and nothing else.
All of the textboxes in active windows doesn't scroll down when the user hits the down button.

Hi Iceplug,

I'm not asking to hog all of the keystrokes. Here is my problem. Our company has tried to achieve a paperless office. And for a company that does data entry, this is no easy task. Teams are developing individual control modules. Mine is a composite control that is to display a scanned image. A document that must then be read by a data entry person, interpreted, and data then needs to be entered in other people's controls. There are multiple controls on a single form.

The problem is that my control, which displays the image must allow a data entry person to scroll through the page. They need to be able to adjust the image size so they can read the document and move through the image. They do not like the mouse. Data entry people prefer all keyboard strokes because it's faster for them. So to toggle through the page, I've written some methods in my control to move up and down, left and right through the page.

However I need to be able to move through the page WITHOUT requiring my control from having the focus. My control only displays the image. Other controls are responsible for handling the data entry. I would prefer not to do it at the form level because my "document viewer" is used on several data entry forms and to have to code the same repetitive keydown event for each form doesn't make sense, not to mention the fact that that's tasked to another person, and it creates a distinct problem of not being object oriented.

My control, must capture all keystrokes, and determine if it's a keystroke to move the image up, down, left, right. (Basically hoping to assign these to function keys or keyboard combinations.).

Anyways, that's my problem, which is why my control needs to receive all the keystrokes and determine if it's one of the key's pressed. Unless you know of a better way to trap an exact key such as F4 being pressed. Any solution?
 
mhsueh001 said:
Ok, this is basically the direction I started heading. How do I go about this?

If you set KeyPreview to True, the form will get all key messages before any control will.

Sounds like over kill to go the other way.
 
jayceepoo said:
If you set KeyPreview to True, the form will get all key messages before any control will.

Sounds like over kill to go the other way.

While I agree it maybe overkill, I believe it maybe the only way to go so I can encapsulate everything into my control, without forcing another coder to code the requirements into their code. It also modularizes it.

I've got it working and found my solution here:

http://www.developer.com/net/net/print.php/11087_2193301_1
 
Back
Top