BlackStone Posted December 20, 2004 Posted December 20, 2004 I am writing a custom control, and I was wondering how I could capture the Tab key. I am sure it is a fairly common problem, but I could not seem to find it on google. Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Administrators PlausiblyDamp Posted December 21, 2004 Administrators Posted December 21, 2004 Are you doing this in a windows or web based application? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BlackStone Posted December 22, 2004 Author Posted December 22, 2004 (edited) Windows. Also, it seems the arrow keys aren't being captured either. Edit: Moved to correct forum Edited December 22, 2004 by PlausiblyDamp Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Administrators PlausiblyDamp Posted December 22, 2004 Administrators Posted December 22, 2004 How are you trying to capture these keys? Do you have any existing code? You will probably find that you want to try either the KeyUp or KeyDown events rather than the KeyPress event. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BlackStone Posted December 23, 2004 Author Posted December 23, 2004 I am using e.KeyData in OnKeyDown. It does not capture the tab key(it moves to the next tabstop instead). Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
coldfusion244 Posted December 23, 2004 Posted December 23, 2004 I am using e.KeyData in OnKeyDown. It does not capture the tab key(it moves to the next tabstop instead). If you are using a lot of keyboard data, maybe a keyboard hook would work the best for you. Quote -Sean
BlackStone Posted December 23, 2004 Author Posted December 23, 2004 would I have to do something so dramatic?? There is no way I can just set an option somewhere? Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
coldfusion244 Posted December 23, 2004 Posted December 23, 2004 would I have to do something so dramatic?? There is no way I can just set an option somewhere? I just suggested this because you expressed major interest in keypresses. This being the case i assumed you might be doing something that would warrant this. As for the Tab, i'm unsure how to trap it. Quote -Sean
Administrators PlausiblyDamp Posted December 23, 2004 Administrators Posted December 23, 2004 What code do you have in the event handler? If you wish to prevent the tab key performing it's default action you will also need to set e.Cancel = true from within the event handler. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Mike_R Posted December 23, 2004 Posted December 23, 2004 I don't think KeyPress has a Cancel argument? Ok, you can use 'e.Handled = True'. There's still a problem in that the {Tab} key seems to be handled before the Keypress ever arrives at the control and therefore moves focus to the next control without any Control_KeyPress() events firing at all. I think you need to either: (1) Set the .TabStop properties for the Controls in question = False, or (2) Set the Form.KeyPreview property = True, allowing you to trap/process the {Tab} keys there. I think that idea #1 is easier to implement though. Quote Posting Guidelines Avatar by Lebb
Administrators PlausiblyDamp Posted December 23, 2004 Administrators Posted December 23, 2004 e.Handled was what I meant :o - just didn't have VS handy at the time. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BlackStone Posted December 23, 2004 Author Posted December 23, 2004 One of the properties of both TextBox & RichTextBox is AcceptsTab, and I am looking to implement such functionality in my own custom control, which means I don't want the user to have to change the tabstop of every one of the other controls on the same form. Here i just an example of what I am looking for: _ public class Example : Control { public Example() { } protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyData == Keys.Tab) { // Won't ever reach this point. // How can I fix that? MessageBox.Show("Tab caught!"); } else if (e.KeyData == Keys.A) { MessageBox.Show("The letter a is caught!"); } } } Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Tygur Posted December 24, 2004 Posted December 24, 2004 Certain keys, such as Tab get special processing. Because of this, they never reach OnKeyDown. To disable the special processing, you can override ProcessDialogKey and return False for the keys in question: Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean If keyData = Keys.Tab Then Return False Else Return MyBase.ProcessDialogKey(keyData) End If End Function After you do that, they'll reach OnKeyDown just like any other key. Quote
BlackStone Posted December 25, 2004 Author Posted December 25, 2004 I knew there was something like that. Thanks! Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.