ヘボいキー状態判断シングルトン
かなウェルのソースを見ていたら、ヘボいコードを発見。
昔はデザインパターンの本とか読んでたのになあ。
キー押下状態を見るシングルトン。switchとか使っているよ。
KeyState.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ABCS.Classes { public enum EKeyMode { SHIFT, CTRL, ALT, CLICKONLY, NONE } public class KeyState { public static EKeyMode ekeymode = EKeyMode.NONE; public static bool isPressingAlt = false; public static bool isPressingShift = false; public static bool isPressingCtrl = false; public static ComboBox cmbKeyModeOnClick { get; set; } public static void setPressAlt(bool isPress) { KeyState.isPressingAlt = isPress; } public static void setPressShift(bool isPress) { KeyState.isPressingShift = isPress; } public static void setPressCtrl(bool isPress) { KeyState.isPressingCtrl = isPress; } public static void setKeyMode() { if( KeyState.cmbKeyModeOnClick != null ) { switch (KeyState.cmbKeyModeOnClick.Text) { case "Shift": KeyState.ekeymode = EKeyMode.SHIFT; break; case "Ctrl": KeyState.ekeymode = EKeyMode.CTRL; break; case "Alt": KeyState.ekeymode = EKeyMode.ALT; break; default: KeyState.ekeymode = EKeyMode.CLICKONLY; break; } } } public static bool isNowLaunchCondition() { switch (KeyState.ekeymode) { case EKeyMode.SHIFT: if (KeyState.isPressingShift) { return true; } else { return false; } break; case EKeyMode.CTRL: if (KeyState.isPressingCtrl) { return true; } else { return false; } break; case EKeyMode.ALT: if (KeyState.isPressingAlt) { return true; } else { return false; } break; case EKeyMode.CLICKONLY: return true; case EKeyMode.NONE: return false; default: return false; } } } }
で、タイマーで監視。
private void timer_for_analyse_Tick(object sender, EventArgs e) { this.timer_for_analyse.Interval = 100; //SHIFT int keycode = win32api.GetKeyState(win32api.VK_CONTROL); if ((keycode & 0x80) == 0x80) { //シングルトンにも状態通知 KeyState.setPressCtrl(true); } else { //シングルトンにも状態通知 KeyState.setPressCtrl(false); } //SHIFT keycode = win32api.GetKeyState(win32api.VK_SHIFT); if ((keycode & 0x80) == 0x80) { //シングルトンにも状態通知 KeyState.setPressShift(true); } else { //シングルトンにも状態通知 KeyState.setPressShift(false); } //ALT keycode = win32api.GetKeyState(win32api.VK_ALT); if ((keycode & 0x80) == 0x80) { //シングルトンにも状態通知 KeyState.setPressAlt(true); this.chkIOHtml.Checked = true; this.chkIOHtml_Click(sender, e); } else { //シングルトンにも状態通知 KeyState.setPressAlt(false); } }