当前位置:主页>delphi7/进销存和数据库> 文章内容

如何判断鼠标单击按钮(Button1Click(Sender: TObject)事件)的时候是否也按下了Ctrl键?

发布时间:2010-01-27 | QQ免费站
1楼: 如何判断鼠标单击按钮(Button1Click(Sender: TObject)事件)的时候是否也按下了Ctrl键?求最佳方法。

字串6

2楼: OnMouseDown事件中有Shift参数 if ssCtrl in Shift then ..... 字串7

3楼: OnKeyDown 按钮的方法判断

字串6

4楼: 测试代码: var CtrlDown: Boolean;//定义一个变量来表示ctrl的状态 procedure TForm1.BitBtn1Click(Sender: TObject); begin if CtrlDown then //如果已经按下ctrl showmessage(‘clicked and ctrl down!‘) else // showmessage(‘clicked !‘); end; procedure TForm1.BitBtn1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin CtrlDown:=ssCtrl in shift ;//捕获ctrl状态,设置ctrl状态变量 end;

字串4

5楼: if (ssCtrl in Shift) then 字串8

6楼: if Bool(GetAsyncKeyState(VK_CONTROL)) then ShowMessage(‘Ctrl‘); 字串5

7楼: To tseug: 按下Ctrl键测试第一次正常,但第二次不按任何键的情况下测试怎么又是第一次的那样!

字串1

8楼: To hongxing_dl: 有部分按钮没有 MouseDown 事件。你的方法并不通用。
字串7

9楼: 顶,问题应该不难的啊,怎么没有人回答了。

字串4

10楼: 那你就自己放一个Timer或者在application.onidle事件里写代码吧: 以TTimer为例,尽量将interval设置小一点(测试通过) var CtrlDown: Boolean;//定义一个变量来表示ctrl的状态 procedure TForm1.Timer1Timer(Sender: TObject); begin CtrlDown:=GetKeyState(VK_CONTROL) < 0 ;//捕获ctrl状态,设置ctrl状态变量 end; procedure TForm1.BitBtn1Click(Sender: TObject); begin if CtrlDown then //如果已经按下ctrl showmessage(‘clicked and ctrl down!‘) else // showmessage(‘clicked !‘); end; 字串1

11楼: if (GetKeyState(VK_CONTROL) and $8000) <> 0 then ShowMessage(‘Ctrl‘);

字串9

12楼: 多谢 tseug。测试通过,请问 $8000 是什么意思? 字串2

13楼: 呵呵, $8000很奇怪吧,其实你看了GetKeyState API说明后,就马上明白啦 最高位为1时,该键是按下的,其他是放开状态 $8000 二进制就是 10000000b 字串3

14楼: 多人接受答案了。

字串4