为什么按键事件没有发送到GWT的TextInputCell.onBrowserEvent()方法? [英] Why isn't the keypress event being sent to GWT's TextInputCell.onBrowserEvent() method?

查看:84
本文介绍了为什么按键事件没有发送到GWT的TextInputCell.onBrowserEvent()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写GWT TextInputCell.onBrowserEvent()方法,以尝试对输入到字段中的字符进行过滤.我如何无法获得用户在keydown事件中键入的实际字符?

I'm overriding the GWT TextInputCell.onBrowserEvent() method to try and apply filtering on the characters that are typed in to the field. How can I can't get the actual character that the user typed in on a keydown event?

我可以使用NativeEvent.getKeyCode()方法来获取键码,但这并不足够,因为我想知道他们键入的特定字符,因此可以将该字符与正则表达式进行比较.据我了解,keyCode只是告诉我他们在键盘上键入的键,因此我无法区分大写字母"A"和小写字母"a".我也可以从keyup事件的InputElement中获取值,但是为时已晚,因为该字符已经被渲染到UI元素.

I can use the NativeEvent.getKeyCode() method to get the key code, but this is insufficient because I want to know the specific character they typed in, so I can compare the character to a regular express. As I understand it, the keyCode just tells me the key they typed on the keyboard, so I can't discriminate between a capital 'A' and a lowercase 'a'. I can also grab the value from the InputElement on the keyup event, but this is too late, because the character has already been rendered to the UI element.

当我呼叫NativeEvent.getCharCode()时,它为我键入的任何字符返回0.我曾在Chrome和Firefox上尝试过此操作.为什么getCharCode()总是返回0?这是一个错误吗?

When I call NativeEvent.getCharCode() it returns 0 for any character I type. I tried this on Chrome and on Firefox. Why does getCharCode() always return 0? Is this a bug?

例如,这是我尝试的操作以及在键盘上键入数字"1"时得到的值:

For example, here is what I tried and the values I get when I type in the number '1' on the keyboard:

event.getKeyCode() - 49
event.getCharCode() - 0
Character.toChars(event.getCharCode())[0] - (a box character because charCode was 0)
String.valueOf(Character.toChars(event.getCharCode())) - (a box character because charCode was 0)
String.valueOf((char) event.getKeyCode()) - 1

更新:

好,所以我看到我需要捕获keypress事件而不是keydown,但是我为每次键入时发生的每个事件打印出每个event.getType()值,但是我再也看不到一个keypress事件已触发.所以我的更新问题是,为什么keypress事件没有发生?而且,如果我无法在TextInputCell.onBrowserEvent()方法中捕获它,是否有另一种方法可以为TextInputCell捕获它.

Update:

OK, so I see that I need to capture the keypress event and not keydown, but I print off every event.getType() value for every event that occurs as I type and I never see a single keypress event triggered. So my updated question is, why isn't the keypress event occurring? And if I can't capture it in the TextInputCell.onBrowserEvent() method, is there a different way to capture it for a TextInputCell.

推荐答案

因此,事实证明AbstractCell采用了表示"consumedEvents"的字符串列表.这基本上限制了子单元类可以处理的事件类型. TextInputCell传入change和keyup事件类型. AbstractInputCell添加焦点,模糊和抠像.因此,TextInputCell无法处理按键本机事件.

So it turns out that the AbstractCell takes a list of strings that represent the "consumedEvents". This basically limits the event types that can be handled by the child cell classes. TextInputCell passes in the change and keyup event types. The AbstractInputCell adds focus, blur, and keydown. Therefore, TextInputCell is not able to handle keypress native events.

我最终在自己的项目中创建了TextInputCell的几乎完全相同的副本,并将keypress类型添加到了构造函数中.这样,我便可以捕获事件并进行想要的过滤.我将在GWT中创建一个请求,以将该功能添加到TextInputCell中.

I ended up creating a near-exact copy of the TextInputCell in my own project and adding the keypress type to the constructor. I was then able to grab the events and do the filtering that I wanted to do. I'm going to create a request in GWT to add this functionality into the TextInputCell.

我还创建了一个功能请求,以将该功能添加到GWT:

I also created a feature request to add this functionality to GWT: http://code.google.com/p/google-web-toolkit/issues/detail?id=5891

这篇关于为什么按键事件没有发送到GWT的TextInputCell.onBrowserEvent()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆