现在的位置: 首页 > 综合 > 正文

[垃圾microsoft, 要啥缺啥] c# metro app keydown issue

2017年10月31日 ⁄ 综合 ⁄ 共 1242字 ⁄ 字号 评论关闭

下面这段代码是用来检测在textbox里 keydown event:

        private void txtTest_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            lblInfo.Text = lblInfo.Text + "," + (int)e.Key;
        }

尝试用keyboard reader来输入card content to textbox (以ENTER结尾),我原以为会把card info的字母or数字逐个显示在textbox里(以逗号隔开),但实际上只捕捉到最后的enter键,之前的key全部都捕捉不到。究其原因,原来和当前keyboard输入法有关!如果输入法是中文,虽然会在TEXTBOX里正确输出CARD INFO,但keydown event就只捕捉到enter键,如果输入法是英文,那么就可以捕捉到所有键。

和keyboard reader完全无关,因为你就算用virtual keyboard来在textbox里输入信息也是同样效果。

如果不是textbox,而是Controls.Page 来trigger keydown event, 因为对于中文输入法,textbox虽然捕捉不到keydown event,但可以通过获取textbox.text来获得card info。if use controls.page to trigger keydown event, then there must be at least one component in the page. passwordBox or button component
is recommended (it looks that no chinese input issue for passwordbox and button), DON'T use textbox because of chinese input issue. 

Note: if no component in the page is focused (for example, you tap any other area in the page), then it will not trigger any keydown event!!! Hence when the component lose focus, focus again.

Therefore, my solution of visitor log project smart reader view as below.

1. in the page, there is only one  "cancel"  button except 2 labels. When the button is clicked, back to parent page. If this button lose focus, focus again.

2. It seems that you needn't set this button focused explicitly, if it is only one component in page, it will be focused automatically when page is loaded.

抱歉!评论已关闭.