HotKey.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:6k
- /*
- * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
- * $Id: HotKey.cs,v 1.2 2005/04/20 08:45:45 okajima Exp $
- */
- using System;
- using System.Text;
- using System.Diagnostics;
- using System.Collections;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- using Poderosa.UI;
- namespace Poderosa.Forms
- {
- /*
- * 儂僢僩僉乕僐儞僩儘乕儖巄掕斉
- *
- * 僉乕傪師偺係庬椶偵嬫暿偡傞丅
- * 1. ModifierKey: Ctrl, Shift, Alt丅壗偐偲慻傒崌傢偣傞僉乕丅
- * 2. CharKey: 暥帤偲娭楢晅偗傜傟偨僉乕丅僥儞僉乕,僗儁乕僗娷傓丅
- * 3. TerminalKey: 僞乕儈僫儖偵偲偭偰堄枴偺偁傞僉乕丅Enter,BS,ESC,Tab,僇乕僜儖僉乕,僼傽儞僋僔儑儞僉乕丅
- * 4. GenericKey: 偦偺懠偺僉乕
- *
- * 儂僢僩僉乕偵桳岠側擖椡偼modifier偵傛傝堎側傝丄
- * modifier側偟偐Shift偺傒丗4偺傒
- * Control傑偨偼Alt偁傝丗 2,3,4
- * 偲側傞丅
- *
- * 妋擣嵪傒夦尰徾丗Shift+F4, Control+F10偱曄側僉乕偑棃傞
- *
- * 僨僶僢僌僆僾僔儑儞偱丄棃偨僉乕偺儕僗僩傪儕僗僩儃僢僋僗偁偨傝偵僟儞僾偡傞婡擻偑偁傞偲傛偄偩傠偆丅
- */
- /// <summary>
- /// HotKey 偺奣梫偺愢柧偱偡丅
- /// </summary>
- internal class HotKey : System.Windows.Forms.TextBox
- {
- /// <summary>
- /// 昁梫側僨僓僀僫曄悢偱偡丅
- /// </summary>
- private System.ComponentModel.Container components = null;
- private TextBox _debugTextBox;
- private Keys _key;
- public HotKey()
- {
- // 偙偺屇傃弌偟偼丄Windows.Forms 僼僅乕儉 僨僓僀僫偱昁梫偱偡丅
- InitializeComponent();
- // TODO: InitForm 傪屇傃弌偟偺屻偵弶婜壔張棟傪捛壛偟傑偡丅
- }
- public TextBox DebugTextBox {
- get {
- return _debugTextBox;
- }
- set {
- _debugTextBox = value;
- }
- }
- public Keys Key {
- get {
- return _key;
- }
- set {
- _key = value;
- Text = FormatKey(value);
- }
- }
- /// <summary>
- /// 巊梡偝傟偰偄傞儕僜乕僗偵屻張棟傪幚峴偟傑偡丅
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Component Designer generated code
- /// <summary>
- /// 僨僓僀僫 僒億乕僩偵昁梫側儊僜僢僪偱偡丅偙偺儊僜僢僪偺撪梕傪
- /// 僐乕僪 僄僨傿僞偱曄峏偟側偄偱偔偩偝偄丅
- /// </summary>
- private void InitializeComponent()
- {
- components = new System.ComponentModel.Container();
- this.ImeMode = ImeMode.Disable;
- }
- #endregion
- protected override bool IsInputKey(Keys key) {
- return false;
- }
- protected override void OnKeyUp(KeyEventArgs args) {
- base.OnKeyUp(args);
- Keys body = _key & Keys.KeyCode;
- if(body==Keys.Menu || body==Keys.ShiftKey || body==Keys.ControlKey) { //modifier偺傒偼擣傔側偄
- _key = Keys.None;
- this.Text = "";
- }
- }
- protected override bool ProcessDialogKey(Keys key) {
- if(_debugTextBox!=null) AppendDebugText(key.ToString()+" "+(int)key);
- string t = FormatKey(key);
- if(t!=null) {
- this.Text = t;
- }
- else
- _key = Keys.None;
- return true;
- }
- private string FormatKey(Keys key) {
- Keys body = key & Keys.KeyCode;
- Keys modifiers = key & Keys.Modifiers;
- //modifier偼忢偵昞帵偡傞
- StringBuilder b = new StringBuilder();
- if((modifiers & Keys.Control)!=Keys.None) {
- b.Append("Ctrl");
- }
- if((modifiers & Keys.Shift)!=Keys.None) {
- if(b.Length>0) b.Append('+');
- b.Append("Shift");
- }
- if((modifiers & Keys.Alt)!=Keys.None) {
- if(b.Length>0) b.Append('+');
- b.Append("Alt");
- }
- if(b.Length>0)
- b.Append('+');
- if(IsCharKey(body)) {
- if(modifiers!=Keys.None && modifiers!=Keys.Shift) {
- if(modifiers==Keys.Alt && (Keys.D0 <= body && body <= Keys.D9))
- _key = Keys.None;
- else {
- b.Append(UILibUtil.KeyString(body));
- _key = key;
- }
- }
- else
- _key = Keys.None;
- }
- else if(IsTerminalKey(body)) {
- if(modifiers!=Keys.None) {
- //僇僗僞儅僀僘晄擻偱屌掕偝傟偨僔儑乕僩僇僢僩僉乕偼搊榐偱偒側偄
- if(modifiers==Keys.Control && IsScrollKey(body))
- _key = Keys.None;
- else {
- b.Append(UILibUtil.KeyString(body));
- _key = key;
- }
- }
- else
- _key = Keys.None;
- }
- else if(IsFunctionKey(body)) {
- b.Append(UILibUtil.KeyString(body));
- _key = key;
- }
- else if(!IsModifierKey(body)) {
- _key = Keys.None;
- }
- return b.ToString();
- }
- private static bool IsCharKey(Keys key) {
- int n = (int)key;
- return ((int)Keys.A <= n && n <= (int)Keys.Z) ||
- ((int)Keys.D0 <= n && n <= (int)Keys.D9) ||
- ((int)Keys.NumPad0 <= n && n <= (int)Keys.NumPad9) ||
- ((int)Keys.OemSemicolon <=n && n <= (int)Keys.Oemtilde) ||
- ((int)Keys.OemOpenBrackets <=n && n <= (int)Keys.OemQuotes) ||
- key==Keys.Divide || key==Keys.Multiply || key==Keys.Subtract || key==Keys.Add || key==Keys.Decimal ||
- key==Keys.Space || key==Keys.Enter;
- }
- private static bool IsModifierKey(Keys key) {
- return key==Keys.Menu || key==Keys.ShiftKey || key==Keys.ControlKey ||
- key==Keys.LMenu || key==Keys.RMenu ||
- key==Keys.LShiftKey || key==Keys.RShiftKey ||
- key==Keys.LControlKey || key==Keys.RControlKey;
- }
- private static bool IsFunctionKey(Keys key) {
- return (int)Keys.F1 <= (int)key && (int)key <= (int)Keys.F24;
- }
- private static bool IsTerminalKey(Keys key) {
- return key==Keys.Escape || key==Keys.Back || key==Keys.Tab ||
- key==Keys.Up || key==Keys.Down || key==Keys.Left || key==Keys.Right ||
- ((int)Keys.F1 <= (int)key && (int)key <= (int)Keys.F12) ||
- key==Keys.Home || key==Keys.End || key==Keys.Next || key==Keys.Prior || key==Keys.PageDown || key==Keys.PageUp ||
- key==Keys.Insert || key==Keys.Delete;
- }
- private static bool IsScrollKey(Keys key) { //TerminalKey偺僒僽僙僢僩偱丄Ctrl偲偺慻傒崌傢偣偱僶僢僼傽偺僗僋儘乕儖傪偡傞
- return key==Keys.Up || key==Keys.Down ||
- key==Keys.Home || key==Keys.End ||
- key==Keys.PageDown || key==Keys.PageUp;
- }
- private void AppendDebugText(string text) {
- string[] data = _debugTextBox.Lines;
- if(data.Length>=5) {
- string[] n = new string[5];
- Array.Copy(data, data.Length-4, n, 0, 4);
- n[4] = text;
- _debugTextBox.Lines = n;
- }
- else {
- string[] n = new string[data.Length+1];
- Array.Copy(data, 0, n, 0, data.Length);
- n[data.Length] = text;
- _debugTextBox.Lines = n;
- }
- }
- }
- }