public KeyChangeTable(string filePath) { Regex regex = new Regex(@"^(\d+),([01])\t+(\d+),(\d+),([01])$"); using (StreamReader reader = new StreamReader(filePath)) { string line; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("#")) { continue; } Match m = regex.Match(line); if (m.Success) { int index = GetTableIndex(int.Parse(m.Groups[1].Value), m.Groups[2].Value == "1"); KeyOperation keyOpe = new KeyOperation(); keyOpe.VkCode = (byte)int.Parse(m.Groups[3].Value); keyOpe.ScanCode = (byte)int.Parse(m.Groups[4].Value); keyOpe.ShiftPressed = m.Groups[5].Value == "1"; if (keyOperationTable_[index] != null) { System.Diagnostics.Debug.WriteLine("??? " + index); System.Windows.Forms.MessageBox.Show("??? " + index); } keyOperationTable_[index] = keyOpe; } } } }
private int KbdHook__LowLevelKeyboardEvent(int nCode, int wParam, ref KeyboardHook.KBDLLHOOKSTRUCT kbdHookInfo, ref bool isCancel) { // shift key が押されたり離されたりした場合... if (kbdHookInfo.vkCode == 160 || kbdHookInfo.vkCode == 161) { // ※shift キーに関する状態を変更(左右の shift が同時に押された場合への対処はしていない...) isShiftPressed_ = (kbdHookInfo.flags & 128) == 0; pressedShiftScanCode_ = kbdHookInfo.scanCode; pressedShiftVkCode_ = kbdHookInfo.vkCode; return(0); } // 物理キーボード以外からの入力は無視する // (無視しないと、このアプリから送信したものまで再処理してしまうので) if ((kbdHookInfo.flags & 16) != 0) { return(0); } if (foregroundHwnd_ == this.Handle) { return(0); } if (regexProcessName_ != null && !regexProcessName_.IsMatch(foregroundProcessName_)) { return(0); } if (regexWindowText_ != null && !regexWindowText_.IsMatch(foregroundWinText_)) { return(0); } System.Diagnostics.Debug.WriteLine("!!"); // 変換処理 bool isPress = (kbdHookInfo.flags & 128) == 0; KeyOperation keyOpe = keyChangeTable_.GetKeyOperation(kbdHookInfo.vkCode, isShiftPressed_); if (keyOpe != null) { SendKey(keyOpe.VkCode, keyOpe.ScanCode, isPress, keyOpe.ShiftPressed); isCancel = true; } return(0); }