在 C# 中捕获键盘敲击

发布于 2024-07-18 14:24:31 字数 301 浏览 14 评论 0原文

您好,

我遇到以下问题 - 以下文本位于富文本框中。

今天的世界[[精彩]]。

如果用户在一个单词之前和之后提供两个括号,如在 Wonder 的情况下,括号中的单词,在这种情况下, Wonder 将更改为链接(绿色)。

我在获取击键顺序时遇到问题,即。 我如何知道用户已输入 [[ ,以便我可以开始解析其后面的其余文本。

我可以通过处理 KeyDown、事件和列表来获取它,但它看起来一点也不优雅。

请让我知道什么是正确的方法。

谢谢, 苏杰

HI,

I have the following problem- the following text is in a rich text box .

The world is [[wonderful]] today .

If the user provides two brackets before and afer a word, as in the case of wonderful , the word in brackets, in this case, wonderful shall change to a link, ( with a green colour ) .

I am having problems in getting the sequence of the keystrokes, ie. how do I know that the user has entered [[ , so I can start parsing the rest of the text which follows it .

I can get it by handlng KeyDown, event, and a list , but it does not look to be elegant at all.

Please let me know what should be a proper way.

Thanks,
Sujay

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟─花易冷 2024-07-25 14:24:31

我可以立即想到你有两种方法。

一是,正如您所建议的,用一个列表来维护当前状态——这个键是一个括号吗? 最后一个键是括号吗?——并即时更新。

另一种方法是简单地处理 TextChanged 事件并重新扫描文本以查找 [[text-here]] 模式并根据需要进行更新。

第一个需要更多的簿记,但对于较长的文本来说会更快。 第二种方法更容易,并且可以使用像样的正则表达式来完成,但是随着文本变长,它会变得更慢。 如果您知道有一些上限,例如 256 个字符,那么您可能没问题。 但如果你期待小说,这可能不是一个好主意。

You have two approaches that I can think of off-hand.

One is, as you suggest, maintain the current state with a list—was this key a bracket? was the last key a bracket?—and update on the fly.

The other approach would be to simply handle the TextChanged event and re-scan the text for the [[text-here]] pattern and update as appropriate.

The first requires more bookkeeping but will be much faster for longer text. The second approach is easier and can probably be done with a decent regex, but it will get slower as your text gets longer. If you know you have some upper limit, like 256 characters, then you're probably fine. But if you're expecting novels, probably not such a great idea.

若无相欠,怎会相见 2024-07-25 14:24:31

我会推荐Google'ing:“richtextbox语法荧光笔”,有很多人已经做到了这一点,并且有很多幕后使其工作。

我敢说,每一个简单的解决方案都有重大缺点。 正确的方法是使用一些已经执行此“语法突出显示”功能的控件并将其扩展到您的语法。 这也很可能是最简单的方法。

您可以在 Codeplex 中搜索免费的 .net 控件。 链接

I would recommend Google'ing: "richtextbox syntax highlighter", there are so many people that have done this, and there is a lot behind the scenes to make it work.

I dare myself to say, that EVERY SINGLE simple solution have major drawbacks. Proper way would be to use some control that already does this "syntax highlighting" and extending it to your syntax. It is also most likely the easiest way.

You can search free .net controls in Codeplex. link

孤独岁月 2024-07-25 14:24:31

我会尝试处理 KeyDown,并检查右括号而不是“]”。 收到一个后,您可以检查文本框中的最后一个字符是否有第二个 ],如果存在,只需替换掉最后几个字符。

这消除了维护状态(即:列表)的需要。 一旦输入第二个],该块就会立即变为链接。

I would try handling the KeyDown, and checking for the closing bracket instead "]". Once you receive one, you could check the last character in your text box for the second ], and if it's there, just replace out the last few characters.

This eliminates the need for maintaining state (ie: the list). As soon as the second ] was typed, the block would change to a link instantly.

愿得七秒忆 2024-07-25 14:24:31

我认为保留一份清单会相当复杂。 如果用户键入“[”字符,单击文本中的其他位置,然后再次键入“[”字符,该怎么办? 然后,用户在文本的完全不同部分键入了两个连续的“[”字符。 此外,您可能还希望能够处理从剪贴板插入的文本。

我认为最安全的方法是分析全文,并使用正则表达式或其他技术从该上下文中执行应该执行的操作。

Keeping a list will be rather complex I think. What if the user types a '[' character, clicks somewhere else in the text and then types a '[' character again. The user has then typed two consecutive '[' characters but in completely different parts of the text. Also, you may want to be able to handle text inserted from the clipboard as well.

I think the safest way is to analyze the full text and do what should be done from that context, using RegEx or some other technique.

第七度阳光i 2024-07-25 14:24:31

(抱歉,还没有足够的声誉来添加评论,因此必须添加新答案)。 正如 jeffamaphone 所建议的,我会处理 TextChanged 事件并每次重新扫描文本 - 但为了保持成本不变,只需扫描当前光标位置前面的几个字符,而不是读取整个文本。

尝试拦截击键并维护内部状态是一种糟糕的方法 - 您对发生的事情的了解很容易与您正在监视的控件不同步并导致奇怪的问题。 (以及如何处理点击?Alt-tab?粘贴?箭头键?其他应用程序抓住焦点?有太多特殊情况需要担心......)

(Sorry, don't have enough reputation to add comments yet, so have to add a new answer). As suggested by jeffamaphone I'd handle the TextChanged event and rescan the text each time - but to keep the cost constant, just scan a few characters ahead of the current cursor position instead of reading the entire text.

Trying to intercept the keystrokes and maintain an internal state is a bad approach - it is very easy for your idea of what has happened to get out of sync with the control you are monitoring and cause weird problems. (and how do you handle clicks? Alt-tab? Pastes? arrow keys? Other applicatiosn grabbing focus? Too many special cases to worry about...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文