• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2099
  • Last Modified:

VK_DOWN, VK_UP in buttons/check boxes

Does anyone know how to get the VK_DOWN and VK_UP values in the on key down event to come through for key?  I'm using Delphi 5.

0
toesable
Asked:
toesable
  • 3
  • 3
1 Solution
 
keashFCommented:
only components that can have a keyboard focus will get a vk_down or vk_up or etc.
such components are things like TEdit, TMemo,...
buttons and forms do not work for this, but you can use the keypreview property of the form (set it to true) to get all key-events that occur in the form's child-components (though it's probably of no use to you)

i also tried to catch the original windows key down event using

procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;

in my form, but it doesn't work either, and as the API Reference says, "only windows with keyboard focus" get this message. so we're right back where we started. :)

hope this helps
keashF
0
 
keashFCommented:
well, ok

here's a possible solution:

install a keyboard hook and catch the keys you want in the associated callback function.
to find out how this works check the win32 reference, search for SetWindowsHookEx. the type must be WH_KEYBOARD and you'll have to define a proper callback function.

cu
keashF
0
 
toesableAuthor Commented:
I see the setwindowshookex in the win32 developement reference but I have never used This and have no examples in the reference.  Any ideas how to try it out?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
keashFCommented:
allright, here it is, just assign the oncreate and onclose
and it'll pop up a message once the down key is pressed.
should work without modifications

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hook:Integer;

function KeyboardProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;


implementation

{$R *.dfm}

function KeyboardProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
  if wparam=vk_down then showmessage('YES!!');
  result:=0; // return 0 to have windows process the message further
             // return non-zero if you´don't want other windows to get this
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  hook:=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,HINSTANCE,0);
  if hook=0 then showmessage('hook failed!');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnhookwindowsHookEx(hook);
end;

end.

i used delphi 7, hope it works for 5 but shouldn't be a prob.
note that LResult, wparam and lparam are essentially integers, but they are C++ types. stdcall is also needed for compatibility. HINSTANCE is the handle for the current module (the program).

cu
keashF
0
 
toesableAuthor Commented:
Thanks, I'll try this out and get back to you tomorrow
0
 
toesableAuthor Commented:
yp, that works.  It's too bad I can just get the keystroke and added it to a custom component, but that's for another time.

Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now