search
HomeBackend DevelopmentC#.Net TutorialExample code sharing for implementing the input method function in C# (pictures and text)

本文主要介绍了C#实现输入法的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧

虽说输入法不是什么新事物,各种语言版本都有,不过在C#不常见;这就会给人一种误会:C#不能做!其实C#能不能做呢,答案是肯定的——三种方式都行:IMM、TSF以及外挂式。IMM这种就是调windows的一些底层api,不过在新版本的windows中基本上已经不能用了,属于一种过时的操作方式。TSF是微软推荐的一种新方式,不过相对C#资料太少;线上主要的一些都是针对C++的版本资料,当然可以作为借鉴来实现C#版的。我这里主要介绍一种外挂式的(天啦撸,C#可以写外挂?),对于高手来说肯定不值一提,不过也算是实现了外挂及输入法!题外话——C#可以做外挂么?答案是可以的,C#针对windows的api编程资料还是很多的,下面就简单的介绍一下面可能要使用到的api:

安装了一个钩子,截取鼠标键盘等信号


public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

停止使用钩子

public static extern bool UnhookWindowsHookEx(int idHook);

通过信息钩子继续下一个钩子

public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

线程钩子需要用到

static extern int GetCurrentThreadId();

使用WINDOWS API函数代替获取当前实例的函数,防止钩子失效

public static extern IntPtr Get<a href="http://www.php.cn/code/8212.html" target="_blank">Module</a>Han<a href="http://www.php.cn/wiki/596.html" target="_blank">dl</a>e(<a href="http://www.php.cn/wiki/57.html" target="_blank">string</a> name);

转换指定的虚拟键码和键盘状态的相应字符或字符


public static extern int ToAscii(int uVirtKey, //[in] 指定虚拟关键代码进行翻译。
int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压)
byte[] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。
在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。
byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。
int fuState);

1.有了以上的这些api基本上就可能实现鼠标键盘的监控或者锁定等;那么首先要安装钩子:


// 安装键盘钩子 
public void Start()
  {
   if (hKeyboardHook == 0)
   {
    KeyboardHookProcedure = new HookProc(KeyboardHookProc);
    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, 
    GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
    //如果SetWindowsHookEx失败
    if (hKeyboardHook == 0)
    {
     Stop();
     throw new Exception("安装键盘钩子失败");
    }
   }
  }

2.安装完后就要对获取到钩子进行处理:


private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
  {
   // 侦听键盘事件
   if (nCode >= 0 && wParam == 0x0100)
   {
    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
    #region 开关
    if (MyKeyboardHookStruct.vkCode == 20 || MyKeyboardHookStruct.vkCode == 160 || MyKeyboardHookStruct.vkCode == 161)
    {
     isLocked = isLocked ? false : true;
    }
    #endregion
    #region
    if (isLocked)
    {
     if (isStarted && MyKeyboardHookStruct.vkCode >= 48 && MyKeyboardHookStruct.vkCode <= 57)
     {
      var c = int.Parse(((char)MyKeyboardHookStruct.vkCode).ToString());
      OnSpaced(c);
      isStarted = false;
      return 1;
     }
     if (isStarted && MyKeyboardHookStruct.vkCode == 8)
     {
      OnBacked();
      return 1;
     }
     if ((MyKeyboardHookStruct.vkCode >= 65 && MyKeyboardHookStruct.vkCode <= 90) || MyKeyboardHookStruct.vkCode == 32)
     {
      if (MyKeyboardHookStruct.vkCode >= 65 && MyKeyboardHookStruct.vkCode <= 90)
      {
       Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
       KeyEventArgs e = new KeyEventArgs(keyData);
       KeyUpEvent(this, e);
       isStarted = true;
      }
      if (MyKeyboardHookStruct.vkCode == 32)
      {
       OnSpaced(0);
       isStarted = false;
      }
      return 1;
     }
     else
      return 0;
    }
    #endregion
   }
   return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
  }

上面一些数字,对于刚入门的同学来说也不是什么问题,一看就明白是对哪些键做的操作。

3.停止钩子


public void Stop()
  {
   bool retKeyboard = true;
   if (hKeyboardHook != 0)
   {
    retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
    hKeyboardHook = 0;
   }
   if (!(retKeyboard))
    throw new Exception("卸载钩子失败!");
  }

4.注册事件


 private void WordBoard_Load(object sender, EventArgs e)
   {
    Program.keyBordHook.KeyUpEvent += KeyBordHook_KeyUpEvent;
    Program.keyBordHook.OnSpaced += KeyBordHook_OnSpaced;
    Program.keyBordHook.OnBacked += KeyBordHook_OnBacked;
   }

5.根据输入内容显示并进行转换


private void ShowCharatar()
  {
   this.listView1.BeginInvoke(new Action(() =>
   {
    label1.Text = keys;
    try
    {
     this.listView1.Items.Clear();
     var arr = CacheHelper.Get(keys);
     if (arr != null)
      for (int i = 0; i < (arr.Length > 10 ? 9 : arr.Length); i++)
      {
       this.listView1.Items.Add((i + 1) + "、" + arr[i]);
      }
    }
    catch
    {
     label1.Text = keys = "";
    }
   }));
  }

6.显示输入


 private void KeyBordHook_KeyUpEvent(object sender, KeyEventArgs e)
   {
    keys += e.KeyCode.ToString().ToLower();
    this.ShowCharatar();
   }

7.空格上屏


private void KeyBordHook_OnSpaced(int choose)
  {
   try
   {
    if (CacheHelper.ContainsKey(keys))
    {
     if (choose > 0)
     {
      choose = choose - 1;
     }
     Program.keyBordHook.Send(CacheHelper.Get(keys)[choose]);
     label1.Text = "";
     this.listView1.Clear();
    }
   }
   catch
   {
   }
   keys = "";
  }

8.将数据发送到激活的输入框中


public void Send(string msg)
  {
   if (!string.IsNullOrEmpty(msg))
   {
    Stop();
    SendKeys.Send("{RIGHT}" + msg);
    Start();
   }
  }

9.back键回退


private void KeyBordHook_OnBacked()
  {
   if (!string.IsNullOrEmpty(keys))
   {
    keys = keys.Substring(0, keys.Length - 1);
   }
   this.ShowCharatar();
  }

当然这里还可以使其他键来完善更多的功能,例如拼音的分页处理等

至于什么五笔、拼音就要使用词库来解决了;其中五笔比较简单,拼音就非常复杂了,各种分词、联想等...这里以五笔为主,拼音为单拼来实现基本的输入功能;所以不需要什么高深算法,简单使用MemoryCache就轻松高效搞定(有兴趣的可以来https://github.com/yswenli/Wenli.IEM 上完善)

10.键词转换


/*****************************************************************************************************
 * 本代码版权归@wenli所有,All Rights Reserved (C) 2015-2017
*****************************************************************************************************
 * CLR版本:4.0.30319.42000
 * 唯一标识:8ebc884b-ee5f-45de-8638-c054b832e0ce
 * 机器名称:WENLI-PC
 * 联系人邮箱:wenguoli_520@qq.com
*****************************************************************************************************
 * 项目名称:$projectname$
 * 命名空间:Wenli.IEM
 * 类名称:CacheHelper
 * 创建时间:2017/3/3 16:18:14
 * 创建人:wenli
 * 创建说明:
*****************************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Windows.Forms;
namespace Wenli.IEM.Helper
{
 public static class CacheHelper
 {
  static MemoryCache _wubiCache = new MemoryCache("wubi");
  static MemoryCache _pinyinCache = new MemoryCache("pinyin");
  static CacheHelper()
  {
   var path = Application.StartupPath + "\\Win32\\world.dll";
   var arr = File.ReadAllLines(path);
   foreach (string item in arr)
   {
    var key = item.Substring(0, item.IndexOf(" "));
    var value = item.Substring(item.IndexOf(" ") + 1);
    _wubiCache.Add(key, (object)value, DateTimeOffset.MaxValue);
   }
   //
   path = Application.StartupPath + "\\Win32\\pinyin.dll";
   arr = File.ReadAllLines(path);
   foreach (string item in arr)
   {
    var key = item.Substring(0, item.IndexOf(" "));
    var value = item.Substring(item.IndexOf(" ") + 1);
    _pinyinCache.Add(key, (object)value, DateTimeOffset.MaxValue);
   }
  }
  public static string[] Get(string key)
  {
   if (!string.IsNullOrEmpty(key))
   {
    var str = string.Empty;
    try
    {
     if (_wubiCache.Contains(key))
      str = _wubiCache[key].ToString();
    }
    catch { }
    try
    {
     if (_pinyinCache.Contains(key))
      str += " " + _pinyinCache[key].ToString();
    }
    catch { }
    if (!string.IsNullOrEmpty(str))
    {
     var arr = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
     for (int i = 0; i < arr.Length; i++)
     {
      if (arr[i].IndexOf("*") > -1)
      {
       arr[i] = arr[i].Substring(0, arr[i].IndexOf("*"));
      }
     }
     return arr;
    }
   }
   return null;
  }
  public static bool ContainsKey(string key)
  {
   if (_wubiCache.Contains(key))
    return true;
   if (_pinyinCache.Contains(key))
    return true;
   return false;
  }
  public static void Clear()
  {
   _wubiCache.Dispose();
   GC.Collect(-1);
  }
 }
}

到此一个基本型的C#版外挂输入法就成功完成了

The above is the detailed content of Example code sharing for implementing the input method function in C# (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor