Are some keyboards more loquacious than others?
Asked Answered
M

2

7

The lead developer says that when he uses my app, his keyboard beeps when he moves between TextBoxes on the TableLayoutPanel via the directional arrow keys.

However, I hear no such aural activity.

Here's my code:

// Had to intercept Up and Down arrows from Windows
private void textBoxPlatypi_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
    TextBox tb = (TextBox)sender;

    if (e.KeyCode.Equals(Keys.Up)) {
        SetFocusOneRowUp(tb.Name);
        return;
    }
    if (e.KeyCode.Equals(Keys.Down)) {
        SetFocusOneRowDown(tb.Name);
        return;
    }
}

private void textBoxPlatypi_KeyDown(object sender, KeyEventArgs e) {
    TextBox tb = (TextBox)sender;

    if (e.KeyCode.Equals(Keys.Left)) {
        SetFocusOneColumnBack(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Right)) {
        SetFocusOneColumnForward(tb.Name);
        e.Handled = true;
        return;
    }
}

..He thought maybe I needed "e.Handled" but that is not available in the PreviewKeyDown event.

Is there a way to suppress the beeping (which apparently occurs only with certain keyboards or specific setups (he's using Windows7, I'm on XP still))?

UPDATE

I've got this code now:

private void textBoxPlatypus1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
    switch (e.KeyCode) {
        case Keys.Down:
        case Keys.Up:
            e.IsInputKey = true;
            break;
    }
}

private void textBoxPlatypus1_KeyDown(object sender, KeyEventArgs e) {
    TextBox tb = (TextBox)sender;

    if (e.KeyCode.Equals(Keys.Up)) {
        SetFocusOneRowUp(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Down)) {
        SetFocusOneRowDown(tb.Name);
        e.Handled = true;
        return;
    }

    if (e.KeyCode.Equals(Keys.Left)) {
        SetFocusOneColumnBack(tb.Name);
        e.Handled = true;
        return;
    }
    if (e.KeyCode.Equals(Keys.Right)) {
        SetFocusOneColumnForward(tb.Name);
        e.Handled = true;
        return;
    }
}

...but he still hears the beeping (I don't).

He's in Alaska and using Windows 7; I'm in California and using XP. I don't know if some combination/mismatch there is the problem...

UPDATED AGAIN

I know this may be shocking to some, but the Alaska/California disconnection has nothing to do with it. I'm now hearing the beeps, too, and it's not from the arrow keys. It's when a value is entered in a TextBox and then, if that text box already has a character, focus is moved to the next textBox and the value is entered there (this is my code that causes this to happen). But the irritating beeping seems to be random - I haven't figured out the pattern for when it beeps (sometimes it does, sometimes it doesn't)...has anybody ever run across anything like that, or, better yet, know how to suppress the beep? All I'm doing is pressing either the "1" or the "2" key above the keyboard.

Maimonides answered 18/5, 2012 at 23:25 Comment(1)
What a great question! I'm probably just jaded from all the time I've spent reading bad ones, but this really impresses me. You've chosen a clever yet relatively descriptive title that actually made me smile. You've succinctly and accurately described your problem, and you've included the relevant code for reproduction purposes. This deserves a lot more upvotes than my one.Punctual
T
3

There is no way in the PreviewKeyDownEvent to Handle / Supress a KeyEvent like there is in the normal KeyDown Event. What the documentation suggests is to set the PreviewKeyDownEventArgs.IsInputKey property to true in order to handle key presses that are not available normally in the KeyDown Event.

From above Link, they are using a button as an example:

Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses... By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.

Thomasinethomason answered 19/5, 2012 at 0:16 Comment(2)
Thanks; I'll look into changing my PreviewKeyDown event as you mention; however, my code DOES work as-is. It's just that I'd like to get the keyboard to "hush up."Maimonides
@Clay I know that it will work, but since you need to stop the propagation of the keystroke you don't have the option to in the PreviewKeyDown eventThomasinethomason
F
1

Try this:

    e.SuppressKeyPress = true;
Farias answered 18/5, 2012 at 23:57 Comment(2)
That is not a member of PreviewKeyDownEventArgsThomasinethomason
No, but it is a member of KeyEventArgs.Farias

© 2022 - 2024 — McMap. All rights reserved.