7

のアプリを開発中ですWindows 8

押されたキーが英数字かどうかを判断しようとしています。クラスはKeyRoutedEventArgsヘルパーを提供していないようです。

私が見落としているものはありますか?ユーザーが文字または数字を入力したかどうかを判断する最善の方法は何ですか?

4

2 に答える 2

0

押されたキーが電話番号 (数字、スペース、ドット、+) に適しているかどうかを確認するサンプルを次に示します。

    using Windows.System;
    ...
    bool isDigitKey(VirtualKey keyValue)
    {
        return ((keyValue >= VirtualKey.Number0 && keyValue <= VirtualKey.Number9)||
            (keyValue >= VirtualKey.NumberPad0 && keyValue <= VirtualKey.NumberPad9));
    }
    private void TextBoxTo_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        // Characters for phone numbers (+, ., space, digits) 
            Boolean isCharFiltered = !isDigitKey(e.Key)        && // Digits
                                 (e.Key != VirtualKey.Space)   && // Space
                                 (e.Key != VirtualKey.Add)     && // + on NumPad
                                 ((int)e.Key != 0xbb)          && // + which is not correctly mapped to VirualKey.Add
                                 (e.Key != VirtualKey.Decimal) && // . on NumPad
                                 ((int)e.Key != 0xbe);            // . which is curiously mapped to Shift
        // Unfortunately, we can't filter à, é and è on a french keyboard because these keys are mapped to Number0, Number2 and Number7

       e.Handled = isCharFiltered;
    }
于 2014-10-02T20:38:54.790 に答える