?
Solved

VK_DOWN, VK_UP in buttons/check boxes

Posted on 2003-02-24
6
Medium Priority
?
2,091 Views
Last Modified: 2012-05-04
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
Comment
Question by:toesable
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:keashF
ID: 8011779
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
 
LVL 1

Expert Comment

by:keashF
ID: 8011803
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
 

Author Comment

by:toesable
ID: 8011983
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
keashF earned 800 total points
ID: 8012255
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
 

Author Comment

by:toesable
ID: 8012386
Thanks, I'll try this out and get back to you tomorrow
0
 

Author Comment

by:toesable
ID: 8018118
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question