新华社 - 键盘文字输入 [英] XNA - Keyboard text input

查看:143
本文介绍了新华社 - 键盘文字输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以基本上我希望能够找回键盘文本。像输入文本到文本字段或什么的。我只写我的游戏窗口。 我已经使用Guide.BeginShowKeyboardInput,因为它打破了一个自给自足的游戏感觉,而事实上,指南总是显示的XBOX按钮似乎没有权利我要么忽视。是的,它是最简单的方式,但我不喜欢它。

Okay, so basically I want to be able to retrieve keyboard text. Like entering text into a text field or something. I'm only writing my game for windows. I've disregarded using Guide.BeginShowKeyboardInput because it breaks the feel of a self contained game, and the fact that the Guide always shows XBOX buttons doesn't seem right to me either. Yes it's the easiest way, but I don't like it.

接下来,我试着使用System.Windows.Forms.NativeWindow。我创建了一个从它继承,并通过它的游戏窗口句柄类,实施的WndProc函数以捕获WM_CHAR(或WM_KEYDOWN)虽然得到的WndProc要求的其他消息,WM_CHAR和WM_KEYDOWN从来没有。所以,我不得不放弃这个想法,而且,我还引用了整个Windows窗体,这意味着不必要的内存占用量的膨胀。

Next I tried using System.Windows.Forms.NativeWindow. I created a class that inherited from it, and passed it the Games window handle, implemented the WndProc function to catch WM_CHAR (or WM_KEYDOWN) though the WndProc got called for other messages, WM_CHAR and WM_KEYDOWN never did. So I had to abandon that idea, and besides, I was also referencing the whole of Windows forms, which meant unnecessary memory footprint bloat.

所以,我最后的想法是创建一个线程级,低级别的键盘钩子。这一直是最成功为止。我得到WM_KEYDOWN消息,(没试过WM_CHAR尚)翻译的虚拟键code的Win32 funcation MapVirtualKey为char。而我得到我的文字! (我只是用Debug.Write印刷此刻)

So my last idea was to create a Thread level, low level keyboard hook. This has been the most successful so far. I get WM_KEYDOWN message, (not tried WM_CHAR yet) translate the virtual keycode with Win32 funcation MapVirtualKey to a char. And I get my text! (I'm just printing with Debug.Write at the moment)

虽然一对夫妇的问题。就好像我有大写锁定,以及反应迟钝shift键。 (当然这不是然而,这只是仅有一个虚拟按键$ C每个按键$ C,因此翻译它只有一个输出),它增加了开销,因为它自身附加到Windows挂钩名单,是没有那么快因为我想它是,但行动迟缓可能是多因Debug.Write。

A couple problems though. It's as if I have caps lock on, and an unresponsive shift key. (Of course it's not however, it's just that there is only one Virtual Key Code per key, so translating it only has one output) and it adds overhead as it attaches itself to the Windows Hook List and isn't as fast as I'd like it to be, but the slowness could be more due to Debug.Write.

有没有其他人走近这一点,并解决它,而不必诉诸一个屏幕键盘?或者有没有人有进一步的想法,我来试试呢?

Has anyone else approached this and solved it, without having to resort to an on screen keyboard? or does anyone have further ideas for me to try?

在此先感谢。

问题问吉米

也许我不理解的问题,但为什么不能使用XNA键盘和KeyboardState班?

Maybe I'm not understanding the question, but why can't you use the XNA Keyboard and KeyboardState classes?

我的评论:

这是因为虽然你可以阅读keystates,你不能获得输入的文字作为以及它是如何由用户输入。

It's because though you can read keystates, you can't get access to typed text as and how it is typed by the user.

因此​​,让我进一步澄清。我希望实现能够读取用户的文字输入,好像他们正在输入到文本框的窗户。键盘和KeyboardState类得到所有钥匙状态,但我不得不地图每个键和组合键的字符重新presentation。这属于过当用户不使用相同的键盘语言和我一样特别的符号(我的双引号是Shift + 2,而美国的键盘有他们的地方附近的返回键)。

So let me further clarify. I want to implement being able to read text input from the user as if they are typing into textbox is windows. The keyboard and KeyboardState class get states of all keys, but I'd have to map each key and combination to it's character representation. This falls over when the user doesn't use the same keyboard language as I do especially with symbols (my double quotes is shift + 2, while american keyboards have theirs somewhere near the return key).

看来我的窗口钩子是要走的路,只是我没有得到WM_CHAR的原因是因为新华社消息泵没有做翻译的消息。

it seems my window hook was the way to go, just the reason I wasn't getting WM_CHAR is because the XNA message pump doesn't do translate message.

添加的TranslateMessage每当我收到了WM _ KEYDOWN消息意味着我得到了我的WM_CHAR消息,然后我用这火的字符输入事件在我MessageHook类,我KeyboardBuffer类已经订阅了,然后该缓冲到文本缓冲器:D(或StringBuilder的,但结果是相同的)

Adding TranslateMessage in whenever I received a WM_KEYDOWN message meant I got my WM_CHAR message, I then used this to fire a character typed event in my MessageHook class which my KeyboardBuffer class had subscribed to, which then buffers that to a text buffer :D (or StringBuilder, but the result is the same)

所以我把它所有的工作,因为我想要的。

So I have it all working as I want.

非常感谢吉米的一个链接到一个非常丰富的主题。

Many thanks to Jimmy for a link to a very informative thread.

推荐答案

也许我不理解的问题,但为什么不能使用XNA键盘和KeyboardState班?

Maybe I'm not understanding the question, but why can't you use the XNA Keyboard and KeyboardState classes?

这篇关于新华社 - 键盘文字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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