0

我在 Windows 10 的通用 Windows 应用程序中有一个 xaml 页面。该页面包含一个文本框。该应用程序使用键盘 MSR(磁条阅读器)来刷卡。

现在,当我专注于文本框并在 MSR 中刷卡时,它会打印文本框中的所有数据。但是,我希望用户只需点击触摸屏键盘上的文本并将其限制在 MSR 中。

请帮忙。

4

1 回答 1

0
    private DispatcherTimer _handledTimer;

    private bool _isHandled = false;

    private bool _isShift = false;

    private void txtEmailKeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Shift)
        {
            _isShift = true;
        }
        else
        {
            if (ToChar(e.Key, _isShift))
            {
                _isHandled = true;
                StartDispatcher();
            }
            _isShift = false;
        }
        e.Handled = _isHandled;
    }


    public void StartDispatcher()
    {
        if (_handledTimer == null)
        {
            _handledTimer = new DispatcherTimer();
            _handledTimer.Interval = new TimeSpan(0, 0, 1);
            _handledTimer.Tick += handledTimerTick;
        }
        _handledTimer.Stop();
        _handledTimer.Start();
    }

    private void handledTimerTick(object sender, object e)
    {
        _handledTimer.Stop();
        _handledTimer = null;
        _isHandled = false;
    }

    private bool ToChar(VirtualKey key, bool shift)
    {
        bool hasSpecificChar = false;
        // look for %
        if (53 == (int)key && shift)
        {
            hasSpecificChar = true;
        }

        // look for ';'
        if (186 == (int)key)
        {
            hasSpecificChar = true;
        }
        return hasSpecificChar;
    }
于 2016-02-04T06:28:49.580 回答