/// <summary> /// 把系统按键转换成终端字节序列 /// 代码参考terminal - terminalInput.cpp /// </summary> /// <param name="key">要转换的系统按键</param> /// <param name="mkey">当前按下的修饰键</param> /// <returns></returns> public byte[] TranslateKey(VTKeys key, VTModifierKeys mkey) { string keyText = string.Empty; //if (mkey == VTModifierKeys.None) //{ // 没有按下修饰键 if (this.isVt52Mode) { // VT52模式下的按键转换,VT52模式下没有Application模式 if (this.IsCursorKey(key)) { keyText = this.keymap[KeyboardMaps.CursorKeyVT52][key]; } else { keyText = this.keymap[KeyboardMaps.KeypadVT52][key]; } } else { // ANSI模式下的按键转换 if (this.IsCursorKey(key)) { // 按下的是光标键 if (this.isApplicationMode) { // 获取Application模式下的光标键的要发送的字节序列 keyText = this.keymap[KeyboardMaps.CursorKeyApplication][key]; } else { // 获取Normal模式下的光标键的要发送的字节序列 keyText = this.keymap[KeyboardMaps.CursorKeyNormal][key]; } } else { // 按下的是其他按键 if (this.isApplicationMode) { keyText = this.keymap[KeyboardMaps.KeypadApplicaion][key]; } else { keyText = this.keymap[KeyboardMaps.KeypadNormal][key]; } } } //} //else //{ // // 按下了修饰键,有可能一次按了多个修饰键 //} // 如果keyText里包含转义字符,那么解析转义字符,转义字符使用十六进制表示,用\x或者\0开头 return(Encoding.ASCII.GetBytes(keyText)); }
/// <summary> /// 判断该按键是否是光标键就 /// </summary> /// <returns></returns> public bool IsCursorKey(VTKeys key) { return(key == VTKeys.UpArrow || key == VTKeys.DownArrow || key == VTKeys.LeftArrow || key == VTKeys.RightArrow); }