Thread: capturing Enter during KeyPress() event

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    capturing Enter during KeyPress() event

    I'm not sure what I'm doing wrong here. Any ideas or help is appreciated. I was thinking of processing the text on a KeyDown event instead, but that didn't work either.

    Code:
    private: System::Void textBox1_KeyDown(System::Object^ sender, KeyEventArgs^ e){
            if (e->KeyCode == Keys::Enter) {
                this->listBox1->Items->Add(this->textBox1->Text);
                ++number_of_listitems;
                this->textBox1->Text = "";
                e->Handled = true;
            }
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can't catch form navigation keys with KeyDown. This is because the IsInputKey method returns false for these keys, indicating that they are not to be sent to KeyDown or KeyPress.

    To get around this you can subclass TextBox and override the IsInputKey method, returning true if Keys.Enter is seen.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    You can't catch form navigation keys with KeyDown. This is because the IsInputKey method returns false for these keys, indicating that they are not to be sent to KeyDown or KeyPress.

    To get around this you can subclass TextBox and override the IsInputKey method, returning true if Keys.Enter is seen.
    Ok thanks maybe it would be easier just to use the base IsInputKey and treat the tab etc the same as Enter for my purpose that would work Thanks!!

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Well I have been struggling with this all day, and searching google to no avail here is what I did as suggested

    Code:
    public ref class EnterTextBox : System::Windows::Forms::TextBox{
       public:virtual bool IsInputKey(Keys keydata) override{
                     if (keydata == Keys::Enter)
                         return true;
                     else
                        return TextBox::IsInputKey(keydata);
                 }
                                   
        };
    I subclassed TextBox and override IsInputKey
    Then I changed textBox1 to type EnterTextBox

    and processed my KeyPressEvent as follows:
    Code:
    private: System::Void textBox1_KeyPress(System::Object^ sender, KeyEventArgs^ e){
            if (e->KeyCode == Keys::Enter) {
                this->listBox1->Items->Add(this->textBox1->Text);
                ++number_of_listitems;
                this->textBox1->Text = "";
                e->Handled = true;
            }
    }
    This is simply supposed to add the text in the textbox to a listbox when the enter key is pressed. The code compiles but still doesn't seem to process Enter as the system bell ring sounds when enter is pressed.

    Signed, Stumped

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Have you tried setting the AcceptsReturn property to true?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    Have you tried setting the AcceptsReturn property to true?
    Just tried that same thing I think AcceptsReturn property is for carriage returns but no it didn't affect anything

    The funny thing I saw a page when I was first looking at how to do this in C# and for the life of me I can't find it again lol, but I think this is essentially what they did
    Last edited by Troy Curless; 09-23-2012 at 08:15 PM.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Troy Curless View Post
    The funny thing I saw a page when I was first looking at how to do this in C# and for the life of me I can't find it again lol, but I think this is essentially what they did
    Maybe here?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Yes that was it,
    I tried using KeyDown as well as KeyPressed and checked to make sure I marked the event as handled as state. I don't know if I translated the C# correctly in my code, but I wasn't translating just going off what I remembered in reading the link

    I'm glad you found it though now I can bookmark it for a refrence but I'm still stumped. I do appreciate you taking the time to assist me, I'm surprised this isn't common knowledge as no one likes switching between mouse and keyboard when entering text into a text for adding to a collection with a add button

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    Code:
    private: System::Void textBox1_KeyDown(System::Object^ sender, KeyEventArgs^ e){       
    if (
    Code:
    e->KeyCode == Keys::F1 && (e->Alt || e->Control || e->Shift)) {
                this->listBox1->Items->Add(this->textBox1->Text);
                ++number_of_listitems;
                this->textBox1->Text = "";
                e->Handled = true;        }
    }
    very strange I did a little test and tried to process the F1 key instead of EnterKey and that didn't work either.

    I know the code that adds the Text to the listBox is correct as this is the exact code I use when the Add button is clicked with the mouse

    Again tried it with KeyDown too
    Last edited by Troy Curless; 09-24-2012 at 02:20 AM.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    I think I just realized this code is an example of the Adapter design pattern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to specify a specific KeyPress event?
    By Diablo02 in forum C# Programming
    Replies: 4
    Last Post: 11-20-2007, 08:50 PM
  2. Capturing Enter Key in Edit Boxs!!
    By poopy_pants2 in forum Windows Programming
    Replies: 6
    Last Post: 12-22-2002, 02:38 PM
  3. Capturing 'enter' key in combo box
    By Robert602 in forum Windows Programming
    Replies: 6
    Last Post: 11-17-2002, 06:46 AM
  4. Capturing Event
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 06-27-2002, 01:07 PM
  5. Capturing The Enter Key
    By Sebastiani in forum Windows Programming
    Replies: 18
    Last Post: 11-01-2001, 03:22 PM