delphi7深入了解keybd_event

delphi7深入了解keybd_event

    //模拟Ctrl+回车(Enter)键,实现在QQ中发送消息
    keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
    keybd_event(13, MapVirtualKey(13, 0), 0, 0);
    keybd_event(13, MapVirtualKey(13, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);

(1) 显示桌面:

很多软件有显示桌面的功能, 并且大家的方法都是遍历窗口, 然后让它们最小化, 其实 win系统给咱们了一个非常方便的WIN键(就是键盘上在CTRL键和ALT键之间的那个带win标志的按键), 利用它, 可以轻松的完成显示桌面的功能.

keybd_event(VK_LWIN, 0, 0 ,0);
keybd_event(‘M’, 0, 0 ,0);
keybd_event(‘M’, 0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0);

其他的操作也类似, 比如直接显示开始的运行,就把上面的’M’换成’R’即可。

直接 keybd_event(VK_LWIN, 0, 0 ,0);
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0);

直接显示“开始”对话框了。

  1. 实现快速的全选

很多的时候,比如listctrl实现全选,你可以用listctrl循环设置每一项的状态为选中,多罗索的事情呀。用快捷键试一试CTRL+A
keybd_event(VK_CONTROL, (BYTE)0, 0 ,0);
keybd_event(‘A’,(BYTE)0, 0 ,0); //此处可以用 ‘A’, (BYTE)65, 用’a’不起作用.
keybd_event(‘A’, (BYTE)0, KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0);

  1. 执行某些特殊的键,比如数字键,大小写,下面是数字键的例子

bool bState=true; //true为按下NumLock,false反之
BYTE keyState[256];

GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1)) )
{
// Simulate a key press
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );

// Simulate a key release
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}

4) 你想CTRL+ALT+DELETE三键一起按下,

keybd_event(VK_CONTROL, 0, 0 ,0);
keybd_event(VK_MENU,0, 0 ,0);
keybd_event(VK_DELETE,0, 0 ,0);

keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_MENU,0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_DELETE,0, KEYEVENTF_KEYUP ,0);

5) Window2000/NT/XP已经不提倡用这个函数了,上面的方法只是为了让大家开阔一下思路,怎么替代呢,呵呵,看下面,所以上面的所有代码都可以用这个来完成

INPUT input[4];
memset(input, 0, sizeof(input));

input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;

input[0].ki.wVk = input[3].ki.wVk = VK_LWIN;
input[1].ki.wVk = input[2].ki.wVk = ‘R’;

//释放
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[0].ki.time = input[1].ki.time = input[2].ki.time = input[3].ki.time = GetTickCount();

SendInput(4, input, sizeof(INPUT));

====================

附WIN键的部分快捷键:

WIN键+D=快速的切到桌面,再次点击返回

WIN键+E=快速打开资源管理器

WIN键+R=“运行”。

WIN键+M=全部视窗最小化。

WIN键+Shift+M=取消全部视窗最小化。

WIN键+F1=Help。

WIN键+F=“寻找”。

WIN键+Ctrl+F=显示“查找电脑”。

WIN键+Tab=切换工作列的程式。

WIN键+Break=显示系统内容。

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, Spin;
 
type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure TimerTimer(Sender: TObject);
    procedure SpinEditChange(Sender: TObject);
    procedure CheckBoxClick(Sender: TObject);
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
implementation{$R *.dfm}
 
const Minute=30;//时间间隔——分钟
      Interval=100;//用于等待保存对话出现的时间——毫秒(根据机器具体性能而定)
var x:integer;//用于区分"另存为"的下拉框
    First:boolean;//是否第一次保存
    Timer: TTimer;
    LabeledEdit:TLabeledEdit;
    SpinEdit:TSpinEdit;
    CheckBox:TCheckBox;
    Label1:TLabel;
 
procedure SimulationKey(key_1:word; alt:word=0; key_2:word=0);//模拟按键
begin
  if alt>0 then
    keybd_event(alt,0,KEYEVENTF_EXTENDEDKEY or 0,0);
  keybd_event(key_1,0,KEYEVENTF_EXTENDEDKEY or 0,0);
  keybd_event(key_1,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
  if key_2>0 then begin
    keybd_event(key_2,0,KEYEVENTF_EXTENDEDKEY or 0,0);
    keybd_event(key_2,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
  end;
  if alt>0 then
    keybd_event(alt,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
end;
 
function EnumChildWndProc(aHwnd:LongInt;AlParam:lParam):boolean;stdcall;//枚举窗体内组件
var WndClassName:array[0..254] of Char;
    WndCaption:array[0..254] of Char;
begin
  GetClassName(aHwnd,wndClassName,254);
  GetWindowText(aHwnd,WndCaption,254);
  if string(wndClassName)='RichEdit20W' then begin//控件为下拉框类型
    inc(x);
    if x=2 then begin//设置路径和文件名的下拉框
      sleep(Interval);
      SendMessage(aHwnd,WM_SETTEXT,0,LongInt(pchar('c:\'+LabeledEdit.Text+'.Doc')));//保存到 C 盘跟
      sleep(Interval);
      SimulationKey(83,VK_MENU);//保存
      result:=false;
      exit;
    end;
  end;
  result:=true;
end;
 
procedure TForm1.TimerTimer(Sender: TObject);
var hCurrentWindow:HWND;
    szText:array [0..254] of char;
begin
  x:=0;
  hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);
  while hCurrentWindow<>0 do begin//枚举窗体
    if GetWindowText(hCurrentWindow,@szText,255)>0 then
    begin
      if pos('- Microsoft Word',strpas(@szText))>0 then begin//找到 Word 程序窗体
        SetForegroundWindow(hCurrentWindow);
        if First then begin //如果是第一次运行
          SimulationKey(70,VK_MENU,65);//打开另存为对话
          sleep(Interval);
          SimulationKey(VK_ESCAPE);//取消保存
          sleep(Interval);
          First:=false;
        end;
        SimulationKey(70,VK_MENU,65);//打开另存为对话
        sleep(Interval);
        hCurrentWindow:=FindWindow(nil,pchar('另存为'));//查找另存为的窗体
        if hCurrentWindow<>0 then begin//找到
          EnumChildWindows(hCurrentWindow,@EnumChildWndProc,0);//枚举窗体内的组件
          exit;
        end;
      end;
    end;
    hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);//继续查找下一个
  end;
end;
 
procedure TForm1.FormShow(Sender: TObject);
var hCurrentWindow:HWND;
    szText:array [0..254] of char;
begin
  First:=true;
  //利用枚举的方法查找 Word 程序的窗体:
  hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);
  while hCurrentWindow<>0 do begin
    if GetWindowText(hCurrentWindow,@szText,255)>0 then begin
      if pos('- Microsoft Word',strpas(@szText))>0 then begin
        SetForegroundWindow(hCurrentWindow);
        if First then begin
          SimulationKey(70,VK_MENU,65);
          sleep(Interval);
          SimulationKey(VK_ESCAPE);
          sleep(Interval);
          First:=false;
        end;
      end;
    end;
    hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption:='自动保存Word文档';
  Position:=poScreenCenter;
  Height:=203;
  Width:=283;
  LabeledEdit:=TLabeledEdit.Create(self);
  with LabeledEdit do begin
    Parent:=Form1;
    LabeledEdit.Left:=80;
    LabeledEdit.Top:=24;
    LabeledEdit.Text:='测试文件';
    LabeledEdit.EditLabel.Caption:='文件名:';
  end;
  SpinEdit:=TSpinEdit.Create(self);
  with SpinEdit do begin
    Parent:=Form1;
    Left:=80;
    Top:=80;
    Width:=121;
    Value:=Minute;
    OnChange:=SpinEditChange;
  end;
  Label1:=TLabel.Create(self);
  with Label1 do begin
    AutoSize:=false;
    Caption:='保存间隔(分钟):';
    Parent:=Form1;
    Left:=80;
    Top:=63;
    Width:=120;
  end;
  CheckBox:=TCheckBox.Create(self);
  with CheckBox do begin
    Parent:=Form1;
    Left:=80;
    Top:=110;
    Caption:='自动保存';
    OnClick:=CheckBoxClick;
  end;
  Timer:= TTimer.Create(self);
  with Timer do begin
    Enabled:=false;
    Interval:=Minute*60000;
    OnTimer:=TimerTimer;
  end;
end;
 
procedure TForm1.SpinEditChange(Sender: TObject);
begin
  if SpinEdit.Text='' then exit;
  Timer.Interval:=SpinEdit.Value * 60000;
end;
 
procedure TForm1.CheckBoxClick(Sender: TObject);
begin
  Timer.Enabled:=CheckBox.Checked;
  if First then TimerTimer(nil);
end;
 
end.

猜你喜欢

转载自blog.csdn.net/qq_29786017/article/details/89812118