原文:C# Winform制作虚拟键盘,支持中文

          最近在做一个虚拟键盘功能,代替鼠标键盘操作,效果如下:

       实现思路:

         1  构建中文-拼音 数据库,我用的是SQLite数据库,如

              

         2 构建布局,如效果图

代码:

  数据库代码文件  SqlHandler.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Windows.Forms; namespace TestKeyBord
{ public class SqlHandler
{
public static void InitSQLite(string db,string table)
{
try
{
DbName = db;
TableName = table; if (CreateDataBase())
{
_SQLiteCommand = _SQLiteConn.CreateCommand();
_SQLiteCommand.Connection = _SQLiteConn;
DesignerTable();
}
}
catch
{ }
} public static System.Data.ConnectionState SqliteState
{
get { return _SQLiteConn.State; }
} #region 数据成员定义 public static string DbName = "MedicalSystemLog";
public static string TableName = "MedicalLog";
public static string _SQLiteConnString = string.Empty; public static SQLiteConnection _SQLiteConn = new SQLiteConnection(); public static SQLiteCommand _SQLiteCommand = new SQLiteCommand(); #endregion #region 创建数据库文件 public static bool CreateDataBase()
{
try
{
_SQLiteConnString = "Data Source=" + DbName + ".db";
_SQLiteConn = new SQLiteConnection(_SQLiteConnString);
_SQLiteConn.Open();
_SQLiteCommand = _SQLiteConn.CreateCommand();
_SQLiteCommand.Connection = _SQLiteConn; if (File.Exists(DbName + ".db"))
{
return true;
}
}
catch
{
// MessageBox.Show("日志系统加载失败!");
}
return false;
} #endregion /// <summary>
/// 矩阵是否连接
/// </summary>
public static bool MatrixIsConnected = false; #region 创建表 public static void DesignerTable()
{
try
{
if (_SQLiteConn.State != System.Data.ConnectionState.Open)
{
_SQLiteConn.Open();
} List<string> list = new List<string> { };
list.Add("ID VARCHAR(5)");//汉字ID
list.Add("Chinese VARCHAR(5)");//汉字
list.Add("English VARCHAR(10)");//拼音
CreateTabel(TableName, list);
list.Clear();
}
catch
{
// MessageBox.Show("创建日志数据库失败!");
}
} public static bool ClearSystemLog()
{
try
{ if (_SQLiteConn.State != System.Data.ConnectionState.Open)
{
_SQLiteConn.Open();
} if (_SQLiteConn.State == System.Data.ConnectionState.Open)
{ _SQLiteCommand.CommandText = "delete from " + TableName + ";";
_SQLiteCommand.ExecuteNonQuery(); } _SQLiteConn.Close();
} catch (Exception ex)
{
// MessageBox.Show("清除日志失败:" + ex.Message);
return false;
}
return true;
} public static bool InsertData(string cn,string en,string id)
{
try
{ if (_SQLiteConn.State != System.Data.ConnectionState.Open)
{
_SQLiteConn.Open();
} if (_SQLiteConn.State == System.Data.ConnectionState.Open)
{ _SQLiteCommand.CommandText = "insert into " + TableName + " values('" +
id + "','" + cn + "','" + en + "');";
_SQLiteCommand.ExecuteNonQuery(); } _SQLiteConn.Close();
}
catch (Exception ex)
{
// MessageBox.Show("日志写入失败:" + ex.Message);
return false;
}
return true;
} public static List<string[]> GetData(string en)
{
List<string[]> list = new List<string[]> { }; try
{ _SQLiteCommand.CommandText = "select * from " + TableName + " where English='"+en+"';"; using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader())
{ string[] items = new string[] { }; while (reader.Read())
{
items = new string[]
{
reader[0].ToString(),
reader[1].ToString(),
reader[2].ToString(),
};
list.Add(items);
} }
} catch (Exception ex)
{
MessageBox.Show(ex.Message + "=== GetDocInfo() ===" + ex.StackTrace);
}
return list;
} public static List<string> GetZnData(string en)
{ en = en.ToLower(); ; List<string> list = new List<string> { }; try
{ _SQLiteCommand.CommandText = "select * from " + TableName + " where English='" + en + "';";
// MessageBox.Show(_SQLiteCommand.CommandText);
using (SQLiteDataReader reader = _SQLiteCommand.ExecuteReader())
{ string[] items = new string[] { }; while (reader.Read())
{ list.Add(reader["Chinese"].ToString());
} }
} catch (Exception ex)
{
MessageBox.Show(ex.Message + "=== GetDocInfo() 2222 ===" + ex.StackTrace);
}
return list;
} public static void CreateTabel(string tableName,List<string> columes )
{
if (_SQLiteConn.State != System.Data.ConnectionState.Open)
{
_SQLiteConn.Open();
} if (_SQLiteConn.State == System.Data.ConnectionState.Open)
{ string sql = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "';"; _SQLiteCommand.CommandText = sql; if (Convert.ToInt32(_SQLiteCommand.ExecuteScalar()) == 0)//1表示存在,0表示不存
{
sql = string.Empty; foreach (string str in columes)
{
sql += str + ",";
} _SQLiteCommand.CommandText = string.Format(
"CREATE TABLE {0} (" + sql.Substring(0, sql.Length - 1) + ")"
, tableName); _SQLiteCommand.ExecuteNonQuery();
_SQLiteConn.Close();
} }
else
{
MessageBox.Show("创建表失败,请打开数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} public static string PinConvert(string en)
{
string data = "";
string enLow = en.ToLower();
for (int i = 0; i < enLow.Length; i++)
{
if (enLow[i].ToString() == "ā")
{ }
}
return data;
} #endregion
}
}

源码下载地址: http://download.csdn.net/detail/taoerit/9686889

更新 2017-2-13 ,还有个简单的方法 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace TestForm
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(
byte bVk, //定义一个虚据拟键码。键码值必须在1~254之间。
byte bScan, //定义该键的硬件扫描码
int dwFlags,
int dwExtraInfo
); private void button1_Click(object sender, EventArgs e)
{
// 81 表示Q,具体看虚拟键盘表示码
textBox1.Focus();
keybd_event(81, 0, 0, 0); //Q压下
keybd_event(81, 0, 0x02, 0); //Q弹起
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} }
}

虚拟键盘码


C# Winform制作虚拟键盘,支持中文的更多相关文章

  1. C# WinForm制作电子琴键盘

    上一篇 http://hovertree.com/h/bjaf/y8qol2p4.htm 再上一篇的基础上,使用WinForm制作了一个电子琴键盘: 演示地址 http://hovertree.com ...

  2. 利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片

    利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片.代码如下 import java.awt.Color;import java.io.File;import java.util.H ...

  3. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程08:虚拟键盘实现》--本系列完结

    8.虚拟键盘实现 概述: 硬键盘就是物理键盘,平时敲的那种.软键盘是虚拟的键盘,不是在键盘上,而是在"屏幕"上.虚拟按键就是虚拟键盘的一部分,根据功能需求,提供部分按键效果的UI可 ...

  4. Xcode6.1模拟器ios8.1模拟器不能弹出虚拟键盘及虚拟键盘无法切换中文输入的解决办法

    1.不能弹出虚拟键盘的解决办法 模拟器菜单Hardware->Keyboard->Connect Hardware Keyboard取消选中,快捷键commad+shift+K 2.虚拟键 ...

  5. [原创]cocos2d-x研习录-第三阶 特性之按键与虚拟键盘

    Cocos2D-x引擎支持按键事件,它能检测设备的键盘输入并处理相应的事件.而基于不同操作系统的移动设备,可供用户操作的按键数量和功能都存在差异.   Cocos2D-x使用CCKeypadDeleg ...

  6. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

  7. Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法

    前言 我在实验进入linux系统启动xwindow server而不启动KDE GNOME等桌面系统时遇到的问题.只启动x server而不启动桌面系统,在xserver之上运行一个全屏的图形界面程序 ...

  8. 隐藏虚拟键盘,解决键盘挡住UITextField问题

    再正式开始之前,先来介绍一下IOS的键盘类型: 一.键盘风格 UIKit框架支持8种风格键盘 ? 1 2 3 4 5 6 7 8 9 10 typedef enum {      UIKeyboard ...

  9. 收起虚拟键盘的各种方法 -- IOS

    使用虚拟键盘来输入资讯,是 iOS 的重要互动方式之一,虚拟键盘通常会自动出现在可以编辑的 UITextField 或是 UITextView 的编辑事件中,叫出键盘固然容易,但是要把它收起来,可就没 ...

随机推荐

  1. [TypeScript] Create random integers in a given range

    Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int betwee ...

  2. Android Error:(1,N1) 错误: 需要class, interface或enum

    造成这个error的原因是Java文件编码格式不对, 比如可能是你之前这个文件是用GBK写的,后来复制到utf-8环境里编译,而文件里有些是隐藏的字符,很难找出来的. 解决方法是在Notepad++新 ...

  3. Qt 模仿QQ截图 动态吸附直线

    最近在学Qt.学东西怎么能不动手. 就写了些小程序.看QQ截图能够动态吸附直线的功能挺有意思,所以就模仿了一个. 先上效果图 界面很简单..呵呵 移动鼠标,会把鼠标所在最小矩形选中.把没有选中的地方给 ...

  4. Android菜鸟的成长笔记(25)——可爱的小闹钟

    摘要: 这一篇主要使用系统为我们提供的一个服务AlarmManager来制作一个Android小闹钟,同时还涉及到了自定义主题.判断第一次启动应用.自定义动画.对话框.制作指导滑动页面等方面.最后形成 ...

  5. C++生成GIF小结

    声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44020175 近来需要把BMP或Kinect的内存图片多张合成 ...

  6. 三步学会用spring开发OSGI——(第一步:环境篇)

    Spring-DM是什么 Spring-DM 指的是Spring Dynamic Modules. dm Server 是一个完全模块化部署的,基于OSGi的Java服务器,为运行企业Java应用和S ...

  7. bootstrap, boosting, bagging

    介绍boosting算法的资源: 视频讲义.介绍boosting算法,主要介绍AdaBoosing http://videolectures.net/mlss05us_schapire_b/ 在这个站 ...

  8. 【9112】求2的n次方的精确值

    Time Limit: 1 second Memory Limit: 2 MB 问题描述 求2^n的精确值.n由用户输入,0<=n<=3232. Input 输入只有一行,一个正整数n. ...

  9. Android开发之assets目录下资源使用总结

    预前知识: Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问 ...

  10. Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 1

    Source Database DB Name:        zwc Schemas:         HR,OE,PM Version:          10.2.0.4 RAC:       ...