Chinese Input in XNA (3)

Source: Internet
Author: User

Chinese Input in XNA (3)

For personal use only, do not reprint, do not use for any commercial purposes.


The first two parts of the Code are transplanted to XNA Game. The biggest problem is that the underlying WinForm window cannot be accessed and window messages cannot be obtained. Some methods on the Internet Use window handle to create an NativeWindow, and then overload NativeWindow WndProc to obtain the ipvs message. Unfortunately, adding the previous code to this architecture does not have any effect. By the way, it is not a good strategy for Game to encapsulate WinForm in the internal environment. Therefore, we have to directly call the API hook window message.

First, declare the following functions:



[DllImport ("user32.dll", CharSet = CharSet. Unicode)]
Static extern IntPtr CallWindowProc (IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam );

[DllImport ("user32.dll", CharSet = CharSet. Unicode)]
Static extern int SetWindowLong (IntPtr hWnd, int nIndex, int dwNewLong );

Create a static class to complete all message capture tasks:



Public static class InputCaputure
Delegate IntPtr WndProc (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam );

Static bool initialized;
Static IntPtr prevWndProc;
Static WndProc hookProcDelegate;
Static IntPtr hIMC;

Public static void Initialize (GameWindow window)
If (initialized)
Throw new InvalidOperationException ("InputCaputure. Initialize can only be called once! ");

HookProcDelegate = new WndProc (HookProc );
PrevWndProc = (IntPtr) SetWindowLong (window. Handle, GWL_WNDPROC, (int) Marshal. GetFunctionPointerForDelegate (hookProcDelegate ));
HIMC = ImmGetContext (window. Handle );
Initialized = true;

Static IntPtr HookProc (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
Switch (msg)
Case WindowMessage. ImeSetContext:
// Add code here
Return (IntPtr) 1;

Case WindowMessage. ImeStartCompostition:
// Add code here

Return (IntPtr) 0;

Case ............

Return CallWindowProc (prevWndProc, hWnd, msg, wParam, lParam );


Now, move the previous code to HookProc. It will capture and process all the messages we care about, and then pass all the messages to Game.

After porting all the code, you will find several problems. First, although the IME window is successfully activated, the IME does not seem to receive any key messages. Add the following code:




Second, the default IME window is not displayed. Good. This is what we hope. In addition, when the IME is activated, pressing the keyboard and keyboard continuously causes the game to get stuck. Is this a bug in the hook window message? No, so does wow. Finally, some problems may occur in full screen mode. This is mainly because Initialize is called to check whether it is in the correct position.

Through these three articles, you should have been able to write a basic Chinese Input System for the game. Finally, you should pay attention to the following functions:

1. In addition to capturing the string that WM_IME_ENDCOMPOSITION uses ImmGetCompositionString to obtain the synthesis, capturing WM_IME_CHAR and WM_CHAR can also achieve the same effect.

2. GetKeyboardLayout can get messages related to the current keyboard layout.

3. ImmGetIMEFileName can be used to obtain the ID name of the current input method.

4. ImmSetConversionStatus can be used to switch the full-width state.

5. Use WM_CHAR to process common input instead of the Keyboard in Xna. Because the Keyboard in XNA is not designed for the input characters, you cannot obtain the Caps Lock or other States at all. Therefore, it is very troublesome to enter a Common English word.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.