简体   繁体   中英

WinForm Key Up event SHIFT and char

I'd like to process an alphabetic character and underscores. How do I tell what char is typed if a SHIFT is also pressed. Currently, shifted chars are handled by the ELSE clause.

private void txtSearch_KeyUp(object sender, KeyEventArgs e)
{
    if (((int)e.KeyData >= 65 && (int)e.KeyData <= 122) ||
             (e.KeyData.ToString() == "_"))
    {
        System.Diagnostics.Debug.WriteLine(e.KeyData);
        //char thisChar = char excluding SHIFT, Control
        System.Diagnostics.Debug.WriteLine("Process " + thisChar);
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("Throw away a " + e.KeyData);
    }
}
if (e.Shift && (((int)e.KeyData >= 65 && (int)e.KeyData <= 122) || (e.KeyData.ToString() == "_")))
{
    //Code here
}

G'day,

I took a slight different approach:

    private void txtSearch_KeyUp(object Sender, KeyEventArgs E)
    {
        int iKeyData = (int)(E.KeyData);

        if (((E.KeyData.HasFlag(Keys.OemMinus) == true) && (E.Shift == true)) || ((iKeyData >= 65) && (iKeyData <= 122)))
        {
            System.Diagnostics.Debug.WriteLine(E.KeyData);
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Throw away a " + E.KeyData);
        }
    }

I'm not sure how this will work with different keyboard layouts though - might want to look into that too.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM