Unity3d Shielded Windows10 Input Method

Source: Internet
Author: User

On WIN10, if some input methods (such as QQ Input method) are installed, it will cause Unity's keyboard event to be captured by input method and cannot be triggered. You can respond to keyboard events only if you switch the input method to the English state.

The solution is,

1: User actively switch input method, or even uninstall input method

2: Program in non-input state, shielded input method

Because Method 1 is in full-screen state, the user completely does not know whether in the input method hijack, often causes to think is the program bug, therefore uses the method 2

In unity, the authorities did not provide a good solution (Input.imecompositionmode invalid). So you can only use the win API.

One of the most important APIs is setting the input method state:

[DllImport ("Imm32.dll")]
private static extern bool Immsetopenstatus (INTPTR HIMC, bool b);

Where HIMC is the input method handle for the window that is currently being entered, and B is true to open, false to close

HIMC can be obtained by another API function

[DllImport ("Imm32.dll")]
private static extern IntPtr Immgetcontext (INTPTR hwnd);

Where the HWND is the handle to the program window

The method of obtaining this value can be consulted: http://blog.csdn.net/linkrules/article/details/50420797

The entire code is as follows:

usingSystem;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices; Public classwin32help{Private Delegate BOOLWndenumproc (INTPTR hwnd,UINTLParam); [DllImport ("user32.dll", SetLastError =true)]    Private Static extern BOOLEnumWindows (Wndenumproc Lpenumfunc,UINTLParam); [DllImport ("user32.dll", SetLastError =true)]    Private Static externIntPtr GetParent (IntPtr hWnd); [DllImport ("user32.dll")]    Private Static extern UINTGetWindowThreadProcessId (IntPtr hWnd,ref UINTlpdwprocessid); [DllImport ("Kernel32.dll")]    Private Static extern voidSetLastError (UINTDwerrcode); /// <summary>    ///gets the window handle of the current process/// </summary>    /// <returns></returns>     Public StaticIntPtr Getprocesswnd () {varPtrwnd =IntPtr.Zero; varPID = (UINT) process.getcurrentprocess (). Id;//Current Process ID        varBresult = EnumWindows (Delegate(IntPtr hwnd,UINTLParam) {            UINTID =0; if(GetParent (hwnd)! =IntPtr.Zero)return true; GetWindowThreadProcessId (hwnd,refID); if(id! =LParam)return true; Ptrwnd= hwnd;//cache the handle.SetLastError (0);//Set no error            return false;//returns false to terminate the enumeration window}, PID); return(!bresult && marshal.getlastwin32error () = =0) ?PtrWnd:IntPtr.Zero; } [DllImport ("Imm32.dll")]    Private Static externIntPtr Immgetcontext (IntPtr hwnd); [DllImport ("Imm32.dll")]    Private Static extern BOOLimmgetopenstatus (IntPtr HIMC); [DllImport ("Imm32.dll")]    Private Static extern BOOLImmsetopenstatus (IntPtr hIMC,BOOLb); /// <summary>    ///Set IME status/// </summary>    /// <param name= "TF" ></param>     Public Static voidSetimeenable (BOOLTF) {        varHandle =Getprocesswnd (); varHime =Immgetcontext (handle);    Immsetopenstatus (Hime, TF); }    /// <summary>    ///Get IME status/// </summary>    /// <returns></returns>     Public BOOLGetimestatus () {varHandle =Getprocesswnd (); varHime =Immgetcontext (handle); returnImmgetopenstatus (Hime); }}

You can then use the Win32help.setimeenable (false) method in your program to mask the IME, such as:

using Unityengine;
 Public classinputtest:monobehaviour{//Use this for initialization    protected voidStart () {win32help.setimeenable (false); }    //Update is called once per frame    protected voidUpdate () {if(Input.getkey (keycode.a)) {Debug.Log ("Aaaaaaaaaaas"); }    }}

Note: Valid only for Windows systems, WIN10 tests are available, other systems are not tested

Unity3d Shielded Windows10 Input Method

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: info-contact@alibabacloud.com 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.