procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_TAB then if ssShift in Shift then ... else ... end;
不以吧,是用up键实现shift+Tab键的功能哟。if Key = VK_TAB then ????
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if(Key = VK_UP)then begin Key:= 0; Perform(WM_NEXTDLGCTL,1,0); end; if(Key = VK_DOWN)then begin Key:= 0; Perform(WM_NEXTDLGCTL,0,0); end; end;
procedure SimulateKeyBoard(key: Word; const shift: TShiftState; Specialkey: Boolean); type TShiftKeyInfo = record shift: Byte; vkey: Byte; end; byteset = set of 0..7; const shiftkeys: array [1..3] of TShiftKeyInfo = ((shift: Ord(ssCtrl); vkey: VK_CONTROL), (shift: Ord(ssShift); vkey: VK_SHIFT), (shift: Ord(ssAlt); vkey: VK_MENU)); var flag: DWORD; bShift: ByteSet absolute shift; i: Integer; begin for i := 1 to 3 do begin if shiftkeys[i].shift in bShift then keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), 0, 0); end; if specialkey then flag := KEYEVENTF_EXTENDEDKEY else flag := 0; keybd_event(key, MapvirtualKey(key, 0), flag, 0); flag := flag or KEYEVENTF_KEYUP; keybd_event(key, MapvirtualKey(key, 0), flag, 0); for i := 3 downto 1 do begin if shiftkeys[i].shift in bShift then keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), KEYEVENTF_KEYUP, 0); end; end;测试type TForm1 = class(TForm) Label1: TLabel; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private procedure PopupMenuItemsClick(Sender: TObject); public oldHandler: TMessageEvent; procedure AppMessage(var Msg: TMsg; var Handled: Boolean); end;var Form1: TForm1;implementation{$R *.dfm}{ TForm1 } procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean); begin if Msg.Message = WM_KEYDOWN then if Msg.wParam = VK_TAB then begin if GetKeyState(VK_SHIFT) < 0 then Label1.Caption := 'BACKTAB!' else Label1.Caption := 'TAB!'; Handled := true end; end;procedure TForm1.FormCreate(Sender: TObject); begin oldHandler := Application.OnMessage; Application.OnMessage := AppMessage; end;procedure TForm1.FormDestroy(Sender: TObject); begin Application.OnMessage := oldHandler; end;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if KEY = VK_UP then SimulateKeyBoard(VK_TAB, [], False); if Key =VK_Down then SimulateKeyBoard(VK_TAB, [ssShift], False); end;..........测试效果按TAB或UP: Label1.Caption = 'TAB!' 按TAB+Shift或Down: Label1.Caption = 'BACKTAB!'
注意: 你做测试时,一定要保证是FORM响应UP、DOWN、TAB、TAB+SHIFT
if key=VK_DOWN then Perform(WM_NEXTDLGCTL,0,0); if Key=VK_UP then Perform(WM_NEXTDLGCTL,1,0);可以实现。只不过要在edit的keydown中写
怎么现在人类都进化了?form 中有个 key preview 属性, 设成 true, 是第一步, 在 form 的 on key down 事件中写到if key=VK_DOWN then Perform(WM_NEXTDLGCTL,0,0); if Key=VK_UP then Perform(WM_NEXTDLGCTL,1,0);嗯, 楼上大把这么写了, 只不过没说明 KeyPreview
begin
if Key = VK_TAB then
if ssShift in Shift then ...
else ...
end;
begin
if(Key = VK_UP)then
begin
Key:= 0;
Perform(WM_NEXTDLGCTL,1,0);
end;
if(Key = VK_DOWN)then
begin
Key:= 0;
Perform(WM_NEXTDLGCTL,0,0);
end;
end;
type
TShiftKeyInfo = record
shift: Byte;
vkey: Byte;
end;
byteset = set of 0..7;
const
shiftkeys: array [1..3] of TShiftKeyInfo = ((shift: Ord(ssCtrl); vkey: VK_CONTROL),
(shift: Ord(ssShift); vkey: VK_SHIFT),
(shift: Ord(ssAlt); vkey: VK_MENU));
var
flag: DWORD;
bShift: ByteSet absolute shift;
i: Integer;
begin
for i := 1 to 3 do
begin
if shiftkeys[i].shift in bShift then
keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), 0, 0);
end;
if specialkey then
flag := KEYEVENTF_EXTENDEDKEY
else
flag := 0; keybd_event(key, MapvirtualKey(key, 0), flag, 0);
flag := flag or KEYEVENTF_KEYUP;
keybd_event(key, MapvirtualKey(key, 0), flag, 0); for i := 3 downto 1 do
begin
if shiftkeys[i].shift in bShift then
keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0),
KEYEVENTF_KEYUP, 0);
end;
end;测试type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
procedure PopupMenuItemsClick(Sender: TObject);
public
oldHandler: TMessageEvent;
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.Message = WM_KEYDOWN then
if Msg.wParam = VK_TAB then
begin
if GetKeyState(VK_SHIFT) < 0 then
Label1.Caption := 'BACKTAB!'
else
Label1.Caption := 'TAB!';
Handled := true
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
oldHandler := Application.OnMessage;
Application.OnMessage := AppMessage;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
Application.OnMessage := oldHandler;
end;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if KEY = VK_UP then
SimulateKeyBoard(VK_TAB, [], False); if Key =VK_Down then
SimulateKeyBoard(VK_TAB, [ssShift], False);
end;..........测试效果按TAB或UP: Label1.Caption = 'TAB!'
按TAB+Shift或Down: Label1.Caption = 'BACKTAB!'
Perform(WM_NEXTDLGCTL,0,0);
if Key=VK_UP then
Perform(WM_NEXTDLGCTL,1,0);可以实现。只不过要在edit的keydown中写
Perform(WM_NEXTDLGCTL,0,0);
if Key=VK_UP then
Perform(WM_NEXTDLGCTL,1,0);嗯, 楼上大把这么写了, 只不过没说明 KeyPreview