📄 mousekey.pas
字号:
unit MouseKey;interfaceuses Qt, SysUtils, Types, Classes, Variants, QGraphics, QControls, QForms, QDialogs, QStdCtrls, QExtCtrls;type TFrmMain = class(TForm) Memo: TMemo; GrpBoxShift: TGroupBox; ChkBoxShiftKey: TCheckBox; ChkBoxAltKey: TCheckBox; ChkBoxCtrlKey: TCheckBox; ChkBoxButtonLeft: TCheckBox; ChkBoxButtonRight: TCheckBox; ChkBoxButtonMiddle: TCheckBox; GrpBoxButton: TGroupBox; ChkBoxLeftButton: TCheckBox; ChkBoxRightButton: TCheckBox; ChkBoxMiddleButton: TCheckBox; procedure FormClick(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormKeyPress(Sender: TObject; var Key: Char); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); private { Private declarations } procedure DeleteAllFlags(); procedure SetShiftFlags( Shift: TShiftState ); function GetVirtualKey( Key: Word ): string; public { Public declarations } end;var FrmMain: TFrmMain;implementation{$R *.xfm}procedure TFrmMain.FormClick(Sender: TObject);begin{ QDialogs.MessageDlg( 'Information', 'Hello, Kylix-fans.', mtInformation, [ mbOk ], 0, mbOk, nil );}end;procedure TFrmMain.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);var OutMsg: string;begin DeleteAllFlags();// if ssRight in Shift then // if Mouse right button clicked case Button of mbLeft: begin{// This is not needed.// if HitTest( X, Y ) then// if ( X >= 0 ) and ( X < ClientWidth ) and ( Y >= 0 ) and ( Y < ClientHeight ) then// begin OutMsg := 'Hello, Kylix-fans. Mouse Left Button clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Application.MessageBox( OutMsg, 'Information', [ smbOK ], smsInformation );// This puts you into the trouble of many clicks.// QDialogs.MessageDlg( 'Information', OutMsg, mtInformation, [ mbOk ], 0, mbOk, nil );// end;} ChkBoxLeftButton.Checked := true; OutMsg := 'Mouse Left Button is clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Memo.Lines.Add( OutMsg ); SetShiftFlags( Shift ); end; mbRight: begin{ Canvas.Brush.Style := bsClear; OutMsg := 'Mouse Right Button clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Canvas.TextOut( X, Y, OutMsg );} ChkBoxRightButton.Checked := true; OutMsg := 'Mouse Right Button is clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Memo.Lines.Add( OutMsg ); SetShiftFlags( Shift ); end; mbMiddle: begin ChkBoxMiddleButton.Checked := true; OutMsg := 'Mouse Middle Button is clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Memo.Lines.Add( OutMsg ); SetShiftFlags( Shift ); end; end;{ if Button = mbRight then // if Mouse right button clicked begin Canvas.Brush.Style := bsClear; OutMsg := 'You Clicked at ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; Canvas.TextOut( X, Y, OutMsg ); end;}end;procedure TFrmMain.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var OutMsg: string;begin DeleteAllFlags(); Memo.Lines.Add( 'OnKeyDown Event:' ); SetShiftFlags( Shift ); OutMsg := 'You just pressed the key ' + GetVirtualKey( Key ) + '.'; Memo.Lines.Add( OutMsg );// OutMsg := 'You just pressed the key ' + Char( Key ) + '.';// Application.MessageBox( OutMsg, 'Information', [ smbOK ], smsInformation );// Application.MessageBox( 'Down:' + OutMsg, 'Information', [ smbOK ], smsInformation );// QDialogs.MessageDlg( 'Information', OutMsg, mtInformation, [ mbOk ], 0, mbOk, nil );end;procedure TFrmMain.FormKeyPress(Sender: TObject; var Key: Char);var OutMsg: string;begin// After comments these, gets chap0315.bmp{ DeleteAllFlags(); Memo.Lines.Add( 'OnKeyPress Event:' ); OutMsg := 'You just pressed the key ' + Key + '.';// OutMsg := 'You just pressed the key ' + Char(Ord(Key)) + '.';// OutMsg := 'You just pressed the key ' + IntToStr(Ord(Key)) + '.'; Memo.Lines.Add( OutMsg );}// Application.MessageBox( 'Press:' + OutMsg, 'Information', [ smbOK ], smsInformation );// Application.MessageBox( OutMsg, 'Information', [ smbOK ], smsInformation );// QDialogs.MessageDlg( 'Information', OutMsg, mtInformation, [ mbOk ], 0, mbOk, nil );end;procedure TFrmMain.DeleteAllFlags;begin ChkBoxShiftKey.Checked := false; ChkBoxAltKey.Checked := false; ChkBoxCtrlKey.Checked := false; ChkBoxButtonLeft.Checked := false; ChkBoxButtonRight.Checked := false; ChkBoxButtonMiddle.Checked := false; ChkBoxLeftButton.Checked := false; ChkBoxRightButton.Checked := false; ChkBoxMiddleButton.Checked := false;// Comments this to show continuous mouse-move operations Memo.Clear();end;procedure TFrmMain.SetShiftFlags(Shift: TShiftState);begin if ssShift in Shift then begin ChkBoxShiftKey.Checked := true; Memo.Lines.Add( 'Shift is in TShiftState.' ); end; if ssAlt in Shift then begin ChkBoxAltKey.Checked := true; Memo.Lines.Add( 'Alt is in TShiftState.' ); end; if ssCtrl in Shift then begin ChkBoxCtrlKey.Checked := true; Memo.Lines.Add( 'Ctrl is in TShiftState.' ); end; if ssLeft in Shift then begin ChkBoxButtonLeft.Checked := true; Memo.Lines.Add( 'Button Left is in TShiftState.' ); end; if ssRight in Shift then begin ChkBoxButtonRight.Checked := true; Memo.Lines.Add( 'Button Right is in TShiftState.' ); end; if ssMiddle in Shift then begin ChkBoxButtonMiddle.Checked := true; Memo.Lines.Add( 'Button Middle is in TShiftState.' ); end;end;function TFrmMain.GetVirtualKey(Key: Word): string;begin(* Key_Escape = 4096 { $1000 }; Key_Tab = 4097 { $1001 }; Key_Backtab = 4098 { $1002 }; Key_Backspace = 4099 { $1003 }; Key_Return = 4100 { $1004 }; Key_Enter = 4101 { $1005 }; Key_Insert = 4102 { $1006 }; Key_Delete = 4103 { $1007 }; Key_Pause = 4104 { $1008 }; Key_Print = 4105 { $1009 }; Key_SysReq = 4106 { $100a }; Key_Home = 4112 { $1010 }; Key_End = 4113 { $1011 }; Key_Left = 4114 { $1012 }; Key_Up = 4115 { $1013 }; Key_Right = 4116 { $1014 }; Key_Down = 4117 { $1015 }; Key_Prior = 4118 { $1016 }; Key_PageUp = 4118 { $1016 }; Key_Next = 4119 { $1017 }; Key_PageDown = 4119 { $1017 }; Key_Shift = 4128 { $1020 }; Key_Control = 4129 { $1021 }; Key_Meta = 4130 { $1022 }; Key_Alt = 4131 { $1023 }; Key_CapsLock = 4132 { $1024 }; Key_NumLock = 4133 { $1025 }; Key_ScrollLock = 4134 { $1026 }; Key_F1 = 4144 { $1030 }; Key_F2 = 4145 { $1031 }; Key_F3 = 4146 { $1032 }; Key_F4 = 4147 { $1033 }; Key_F5 = 4148 { $1034 }; Key_F6 = 4149 { $1035 }; Key_F7 = 4150 { $1036 }; Key_F8 = 4151 { $1037 }; Key_F9 = 4152 { $1038 }; Key_F10 = 4153 { $1039 }; Key_F11 = 4154 { $103a }; Key_F12 = 4155 { $103b }; Key_F13 = 4156 { $103c }; Key_F14 = 4157 { $103d }; Key_F15 = 4158 { $103e }; Key_F16 = 4159 { $103f }; Key_F17 = 4160 { $1040 }; Key_F18 = 4161 { $1041 }; Key_F19 = 4162 { $1042 }; Key_F20 = 4163 { $1043 }; Key_F21 = 4164 { $1044 }; Key_F22 = 4165 { $1045 }; Key_F23 = 4166 { $1046 }; Key_F24 = 4167 { $1047 }; Key_F25 = 4168 { $1048 }; Key_F26 = 4169 { $1049 }; Key_F27 = 4170 { $104a }; Key_F28 = 4171 { $104b }; Key_F29 = 4172 { $104c }; Key_F30 = 4173 { $104d }; Key_F31 = 4174 { $104e }; Key_F32 = 4175 { $104f }; Key_F33 = 4176 { $1050 }; Key_F34 = 4177 { $1051 }; Key_F35 = 4178 { $1052 }; Key_Super_L = 4179 { $1053 }; Key_Super_R = 4180 { $1054 }; Key_Menu = 4181 { $1055 }; Key_Hyper_L = 4182 { $1056 }; Key_Hyper_R = 4183 { $1057 }; Key_Help = 4184 { $1058 }; Key_Space = 32 { $20 }; Key_Any = 32 { $20 }; Key_Exclam = 33 { $21 }; Key_QuoteDbl = 34 { $22 }; Key_NumberSign = 35 { $23 }; Key_Dollar = 36 { $24 }; Key_Percent = 37 { $25 }; Key_Ampersand = 38 { $26 }; Key_Apostrophe = 39 { $27 }; Key_ParenLeft = 40 { $28 }; Key_ParenRight = 41 { $29 }; Key_Asterisk = 42 { $2a }; Key_Plus = 43 { $2b }; Key_Comma = 44 { $2c }; Key_Minus = 45 { $2d }; Key_Period = 46 { $2e }; Key_Slash = 47 { $2f }; Key_0 = 48 { $30 }; Key_1 = 49 { $31 }; Key_2 = 50 { $32 }; Key_3 = 51 { $33 }; Key_4 = 52 { $34 }; Key_5 = 53 { $35 }; Key_6 = 54 { $36 }; Key_7 = 55 { $37 }; Key_8 = 56 { $38 }; Key_9 = 57 { $39 }; Key_Colon = 58 { $3a }; Key_Semicolon = 59 { $3b }; Key_Less = 60 { $3c }; Key_Equal = 61 { $3d }; Key_Greater = 62 { $3e }; Key_Question = 63 { $3f }; Key_At = 64 { $40 }; Key_A = 65 { $41 }; Key_B = 66 { $42 }; Key_C = 67 { $43 }; Key_D = 68 { $44 }; Key_E = 69 { $45 }; Key_F = 70 { $46 }; Key_G = 71 { $47 }; Key_H = 72 { $48 }; Key_I = 73 { $49 }; Key_J = 74 { $4a }; Key_K = 75 { $4b }; Key_L = 76 { $4c }; Key_M = 77 { $4d }; Key_N = 78 { $4e }; Key_O = 79 { $4f }; Key_P = 80 { $50 }; Key_Q = 81 { $51 }; Key_R = 82 { $52 }; Key_S = 83 { $53 }; Key_T = 84 { $54 }; Key_U = 85 { $55 }; Key_V = 86 { $56 }; Key_W = 87 { $57 }; Key_X = 88 { $58 }; Key_Y = 89 { $59 }; Key_Z = 90 { $5a }; Key_BracketLeft = 91 { $5b }; Key_Backslash = 92 { $5c }; Key_BracketRight = 93 { $5d }; Key_AsciiCircum = 94 { $5e }; Key_Underscore = 95 { $5f }; Key_QuoteLeft = 96 { $60 }; Key_BraceLeft = 123 { $7b }; Key_Bar = 124 { $7c }; Key_BraceRight = 125 { $7d }; Key_AsciiTilde = 126 { $7e }; Key_nobreakspace = 160 { $a0 }; Key_exclamdown = 161 { $a1 }; Key_cent = 162 { $a2 }; Key_sterling = 163 { $a3 }; Key_currency = 164 { $a4 }; Key_yen = 165 { $a5 }; Key_brokenbar = 166 { $a6 }; Key_section = 167 { $a7 }; Key_diaeresis = 168 { $a8 }; Key_copyright = 169 { $a9 }; Key_ordfeminine = 170 { $aa }; Key_guillemotleft = 171 { $ab }; Key_notsign = 172 { $ac }; Key_hyphen = 173 { $ad }; Key_registered = 174 { $ae }; Key_macron = 175 { $af }; Key_degree = 176 { $b0 }; Key_plusminus = 177 { $b1 }; Key_twosuperior = 178 { $b2 }; Key_threesuperior = 179 { $b3 }; Key_acute = 180 { $b4 }; Key_mu = 181 { $b5 }; Key_paragraph = 182 { $b6 }; Key_periodcentered = 183 { $b7 }; Key_cedilla = 184 { $b8 }; Key_onesuperior = 185 { $b9 }; Key_masculine = 186 { $ba }; Key_guillemotright = 187 { $bb }; Key_onequarter = 188 { $bc }; Key_onehalf = 189 { $bd }; Key_threequarters = 190 { $be }; Key_questiondown = 191 { $bf }; Key_Agrave = 192 { $c0 }; Key_Aacute = 193 { $c1 }; Key_Acircumflex = 194 { $c2 }; Key_Atilde = 195 { $c3 }; Key_Adiaeresis = 196 { $c4 }; Key_Aring = 197 { $c5 }; Key_AE = 198 { $c6 }; Key_Ccedilla = 199 { $c7 }; Key_Egrave = 200 { $c8 }; Key_Eacute = 201 { $c9 }; Key_Ecircumflex = 202 { $ca }; Key_Ediaeresis = 203 { $cb }; Key_Igrave = 204 { $cc }; Key_Iacute = 205 { $cd }; Key_Icircumflex = 206 { $ce }; Key_Idiaeresis = 207 { $cf }; Key_ETH = 208 { $d0 }; Key_Ntilde = 209 { $d1 }; Key_Ograve = 210 { $d2 }; Key_Oacute = 211 { $d3 }; Key_Ocircumflex = 212 { $d4 }; Key_Otilde = 213 { $d5 }; Key_Odiaeresis = 214 { $d6 }; Key_multiply = 215 { $d7 }; Key_Ooblique = 216 { $d8 }; Key_Ugrave = 217 { $d9 }; Key_Uacute = 218 { $da }; Key_Ucircumflex = 219 { $db }; Key_Udiaeresis = 220 { $dc }; Key_Yacute = 221 { $dd }; Key_THORN = 222 { $de }; Key_ssharp = 223 { $df }; Key_agrave_lower = 224 { $e0 }; Key_aacute_lower = 225 { $e1 }; Key_acircumflex_lower = 226 { $e2 }; Key_atilde_lower = 227 { $e3 }; Key_adiaeresis_lower = 228 { $e4 }; Key_aring_lower = 229 { $e5 }; Key_ae_lower = 230 { $e6 }; Key_ccedilla_lower = 231 { $e7 }; grave_lower = 232 { $e8 }; acute_lower = 233 { $e9 }; circumflex_lower = 234 { $ea }; diaeresis_lower = 235 { $eb }; Key_igrave_lower = 236 { $ec }; Key_iacute_lower = 237 { $ed }; Key_icircumflex_lower = 238 { $ee }; Key_idiaeresis_lower = 239 { $ef }; th_lower = 240 { $f0 }; Key_ntilde_lower = 241 { $f1 }; Key_ograve_lower = 242 { $f2 }; Key_oacute_lower = 243 { $f3 }; Key_ocircumflex_lower = 244 { $f4 }; Key_otilde_lower = 245 { $f5 }; Key_odiaeresis_lower = 246 { $f6 }; Key_division = 247 { $f7 }; Key_oslash = 248 { $f8 }; Key_ugrave_lower = 249 { $f9 }; Key_uacute_lower = 250 { $fa }; Key_ucircumflex_lower = 251 { $fb }; Key_udiaeresis_lower = 252 { $fc }; Key_yacute_lower = 253 { $fd }; Key_thorn_lower = 254 { $fe }; Key_ydiaeresis = 255 { $ff }; Key_unknown = 65535 { $ffff };*) case Key of// Key_Shift: Result := 'Shift';// Key_Alt: Result := 'Alt';// Key_Control: Result := 'Ctrl'; Key_Escape: Result := 'Esc'; Key_F1: Result := 'F1'; Key_F2: Result := 'F2'; else Result := Char( Key ); end;end;procedure TFrmMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var OutMsg: string;begin DeleteAllFlags(); SetShiftFlags( Shift ); OutMsg := 'Mouse moves to ( ' + IntToStr( X ) + ', ' + IntToStr( Y ) + ' ).'; if ssLeft in Shift then OutMsg := 'Left holds. ' + OutMsg; Memo.Lines.Add( OutMsg );end;end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -