插入位置不跟踪? [英] Caret position not keeping track?

查看:101
本文介绍了插入位置不跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是将密钥代码放在一个数组中,稍后再做一些有趣的事情。因此,我捕获击键,获取插入位置并将关键代码放入数组中(在MooTools的帮助下):

What I'm trying to do is put key codes in an array, to do some interesting stuff with later on. So, I capture the keystrokes, get the caret position and put the key code in the array (with some help from MooTools):

var keyArray = [];
$('form').addEvent('keyup', function(event) {
    var pos = document.activeElement.getCaretPosition();

    keyArray[pos] = event.code;
});

一般来说,这很有用。但是,当我在控制台中显示完整的数组时,我注意到我的数组中有一些未定义的值。进一步探索这一点,我发现当快速打字时,插入位置似乎失去了轨道,或者快速/慢速地响应。我已经做了一个jsfiddle来证明这一点: http://jsfiddle.net/HQVR8/1/

Generally speaking, this works great. However, I noticed some undefined values in my array when showing the complete array in my console. Exploring this further, I found out that when typing quickly, the caret position seems to lose track, or being quick/slow to respond. I've made a jsfiddle to demonstrate this: http://jsfiddle.net/HQVR8/1/

如果你在这个例子中快速输入,你会看到一个插入位置序列,如

If you type quickly in this example, you'll see a caret position sequence like

- 1 - 2 - 3 - 5 - 6 - 6.

但是当慢慢打字,这是

- 1 - 2 - 3 - 4 - 5 - 6. 

当然,现在输入快速的麻烦是我的未定义值我的数组和我覆盖一个数组项。所以结果是不同的。

Of course, the trouble now when typing quickly is that I have a undefined value in my array and I overwrite one array item. So the result is different.

我的问题是,如果我能以某种方式让插入位置跟踪。我尝试使用'native' selectionStart ,但结果是一样的。我还尝试在 keydown 事件中捕获插入位置,并将其放入 keyup 事件中的数组中。没有不同。我想知道是否使用小暂停(即强制用户输入更慢)可能会解决它,但这感觉就像一个黑客,我宁愿一个'正确'的解决方案。希望有一个。

My question is if I can somehow let the caret position keep track. I've tried using the 'native' selectionStart instead, but results are the same. I also tried capturing the caret position in a keydown event and putting it in an array in the keyup event. No difference. I'm wondering if using little pauses (ie. forcing the user to type slower) might solve it, but this feels like a hack and I would prefer a 'proper' solution. Hopefully, there is one.

推荐答案

经过多次测试后,它似乎是特定于的行为KEYUP 。当我使用 keydown 时,我获得一致的序列: http://jsfiddle.net/HQVR8/3/

After some more tests, it appears to be a behavior specific to keyup. When I use keydown I do get a consistent sequence: http://jsfiddle.net/HQVR8/3/

一个缺点是 keydown 落后 keyup ,但在我的设置中,这只是一个小问题。

One disadvantage is that keydown is a step behind keyup when you're doing the 'value collecting' I'm doing, but in my setting, this is only a minor issue.

行为上的差异对我来说很奇怪:当你一次按下四个键时, keyup 显示相同的所有这些的插入位置,而 keydown 显示四个不同的插入位置。这看起来很奇怪,因为你立即按下它们,但也立即按下它们,所以插入符号'读数'应该是相同的。

The difference in behavior appears odd to me: when you press four keys at a time, the keyup displays the same caret position for all of them, while keydown is showing four separate caret positions. This seems odd because you press them at once, but also depress them at once, so the caret 'readings' should be the same.

这篇关于插入位置不跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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