一、目的:在應用osk.exe系統鍵盤時遇到很多不方便,比如有些系統調用不出來等問題,由此開發了一個系統鍵盤仿製osk
二、實現功能
1、目前實現大部分osk鍵盤的輸入功能,還有部分按鍵由於時間關係沒有實現
2、支持組合按鍵shift、ctrl、alt等方式輸入
3、支持大小寫輸入切換
4、支持shift切換複合按鍵
5、支持在外部記事本、網頁等有光標的位置進行輸入
三、示意圖
系統鍵盤部分:
數字鍵盤部分:
四、實現部分
1、採用模擬虛擬按鍵方式輸入按鍵
public static partial class KeyHelper
{
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(
byte bVk, //虛擬鍵值
byte bScan,// 一般爲0
int dwFlags, //這裏是整數類型 0 爲按下,2爲釋放
int dwExtraInfo //這裏是整數類型 一般情況下設成爲0
);
[DllImport("user32.dll")]
public static extern int GetFocus();
}
2、通過禁用窗體獲取焦點方式來進行外部輸入,如記事本
private void KeyBoardWindow_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
IntPtr intPtr = windowInteropHelper.Handle;
int value = -20;
SetWindowLong(intPtr, value, (IntPtr)0x8000000);
}
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nindex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern UInt32 GetWindowLong(IntPtr hWnd, int index);
五、待實現部分
目前Capslock、ctrl、shift、alt的按下狀態和按鍵狀態沒有和外部硬件鍵盤做同步、後續會應用鉤子檢測同步外部硬件鍵盤狀態
有些按鍵如win、fn、~等按鍵存在問題後續更新