cdewhurst
September 13th, 2002, 06:21 AM
I have been developing a user control in C# and I am having problems receiving certain key-press event from the system.
The control is not derived from any other control, but in the control designer I have included a scroll-bar, which is an integral part of the control. The rest of the control is drawn in my own code.
The problem that I am having is that some of the key press events that I want to intercept are being sent directly to the contained scroll-bar control and I have not been able to intercept these messages.
Initally, I could not intercept the right-arrow key press. This problem was conquered by overriding the IsInputKey method of the control. However, I am still unable to intercept a Shift-Right Arrow key press (see how a text-box handles a Shift-Right Arrow key press for a reason why I might want to do something like this).
To re-create this behaviour, create a new project. Then create a user control and using the designer, add a vertical scroll-bar onto the control. Compile the control and then create a form and add this newly created control to the form. Now run the whole project. Click on your control to select it. You will see that when you press Shift-Right Arrow, the focus moves to the scroll-bar.
Adding the code
protected override bool IsInputKey(Keys keyData)
{
if (keyData == Keys.Right)
{
return true ;
}
return base.IsInputKey(keyData) ;
}
will enable me to trap a Right Arrow key press in functions like:
protected override void OnKeyDown(KeyEventArgs e)
{
... handler code here.
}
However, I cannot get this to work for Shift-Right Arrow.
I have tried looking at the functions ProcessCmdKey, ProcessDialogKey, ProcessDialogChar, ProcessKeyMessage, ProcessKeyPreview and ProcessMnemonic. I suspect that the answer lays in returning the correct value from one of these functions, but I have not been able to determine how to provide the correct code to do this.
Is there anybody out there who understands how .NET handles key events that can help me unravel this mystery?
Thanks.
Chris :confused:
The control is not derived from any other control, but in the control designer I have included a scroll-bar, which is an integral part of the control. The rest of the control is drawn in my own code.
The problem that I am having is that some of the key press events that I want to intercept are being sent directly to the contained scroll-bar control and I have not been able to intercept these messages.
Initally, I could not intercept the right-arrow key press. This problem was conquered by overriding the IsInputKey method of the control. However, I am still unable to intercept a Shift-Right Arrow key press (see how a text-box handles a Shift-Right Arrow key press for a reason why I might want to do something like this).
To re-create this behaviour, create a new project. Then create a user control and using the designer, add a vertical scroll-bar onto the control. Compile the control and then create a form and add this newly created control to the form. Now run the whole project. Click on your control to select it. You will see that when you press Shift-Right Arrow, the focus moves to the scroll-bar.
Adding the code
protected override bool IsInputKey(Keys keyData)
{
if (keyData == Keys.Right)
{
return true ;
}
return base.IsInputKey(keyData) ;
}
will enable me to trap a Right Arrow key press in functions like:
protected override void OnKeyDown(KeyEventArgs e)
{
... handler code here.
}
However, I cannot get this to work for Shift-Right Arrow.
I have tried looking at the functions ProcessCmdKey, ProcessDialogKey, ProcessDialogChar, ProcessKeyMessage, ProcessKeyPreview and ProcessMnemonic. I suspect that the answer lays in returning the correct value from one of these functions, but I have not been able to determine how to provide the correct code to do this.
Is there anybody out there who understands how .NET handles key events that can help me unravel this mystery?
Thanks.
Chris :confused: