Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 10 total
Thread OnKeyDown Problem
Fri, Sep 20 2013 4:24 AMPermanent Link

Frederick Chin

Using EWB Trial 1.02 b2...

I have a form with a panel. In the OnKeyDown event of the form, the code is as follows:-

function TfrmMain.frmMainKeyDown(Sender: TObject; Key: Integer; ShiftKey, CtrlKey, AltKey: Boolean): Boolean;
var
  cMenu : Char;
begin   
  cMenu:=chr(Key);
  if (cMenu='1') then
     showmessage('You pressed 1');
  if (cMenu='2') then
     showmessage('You pressed 2');
  Result:=True;
end;

If I press either 1 or 2, nothing happens. I need to click either on the main form or the panel first and then press 1 or 2 and the message is shown.

Q1.   Why is this?
Q2.   Another thing is how do I trap that the numeric pad key has been pressed. Using VK_NUMPAD1-9 does not work.
Q3.   Why is a return value required?

Frederick
Fri, Sep 20 2013 7:14 AMPermanent Link

Matthew Jones

> If I press either 1 or 2, nothing happens. I need to click either
> on the main form or the panel first and then press 1 or 2 and the
> message is shown.

I don't think I quite follow. Clicking on the form or panel will set the focus to
the browser and the content, thus your form. Where was the focus before this click?


/Matthew Jones/
Fri, Sep 20 2013 11:48 AMPermanent Link

Frederick Chin

/*
I don't think I quite follow. Clicking on the form or panel will set the focus to
the browser and the content, thus your form. Where was the focus before this click?
*/

I don't know. After I press F9 to run the project, I pressed either the 1 or 2 key. No message is displayed. I click on the main form and then press 1 and the message is displayed.

Note: Even when I create a new project with just one form and the OnKeyDown event, the same thing happens. (project attached)

Frederick



Attachments: testkey.zip
Fri, Sep 20 2013 3:25 PMPermanent Link

Raul

Team Elevate Team Elevate

>Frederick Chin wrote:
>I don't know. After I press F9 to run the project, I pressed either the 1 or 2 key. No message is displayed. I click >on the main form and then press 1 and the message is displayed.

I think the issue is that the Form is not a normally focusable control so when running the focus is not autoatically assigned to the form.
You could do a workaround for this by adding TPanel, making it fill the form (dock=dtfill) and removing border and caption. Then the keydown on panel should work as expected.

One thing to note is that if you do add another focusable control - like editbox or button - then when those get focus the onmouse fires for them (and not panel).

Raul
Sat, Sep 21 2013 3:55 AMPermanent Link

Frederick Chin

/*
I think the issue is that the Form is not a normally focusable control so when running the focus is not autoatically assigned to the form.
You could do a workaround for this by adding TPanel, making it fill the form (dock=dtfill) and removing border and caption. Then the keydown on panel should work as expected.
*/

I tried the above; same thing unfortunately. I need to click on the panel, then press 1 to get the message. If I press 1 again after I close the message box, nothing happens.

I also thought that the form was not focused and was not responding to my keystrokes. Even when I call "Form1.Setfocus" in the OnShow event of the form and then press 1, the message still does not show.

To make things more strange, if the form is run from Chrome, even clicking the panel first before pressing a key doesn't show a message at all.

/*
One thing to note is that if you do add another focusable control - like editbox or button - then when those get focus the onmouse fires for them (and not panel).
*/

Noted.

Frederick
Mon, Sep 23 2013 1:27 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frederick,

<< I don't know. After I press F9 to run the project, I pressed either the 1
or 2 key. No message is displayed. I click on the main form and then press 1
and the message is displayed.

Note: Even when I create a new project with just one form and the OnKeyDown
event, the same thing happens. (project attached) >>

There's a lot of browser weirdness behind this, so don't count on key events
showing up without at least one focusable control present.  I'll revisit
this, but last time I did there were serious problems with allowing blank
forms to obtain focus.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 23 2013 10:28 PMPermanent Link

Frederick Chin

Tim,

/*
There's a lot of browser weirdness behind this, so don't count on key events
showing up without at least one focusable control present.  I'll revisit
this, but last time I did there were serious problems with allowing blank
forms to obtain focus.
*/

I tried a workaround where I placed a TEdit control on the form and reduced the width/height of the form to hide the control. When the form is shown, I set focus to it and trap the OnkeyDown event of the control. This allowed the key presses to be handled.

However, the form keeps trying to show the control. Is there a way to make the form's size stay the same so that the control remains hidden?

Frederick



Attachments: testkey.zip
Mon, Sep 30 2013 4:01 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frederick,

<< However, the form keeps trying to show the control. Is there a way to
make the form's size stay the same so that the control remains hidden? >>

If you're seeing a "scroll to the focused control" behavior, then no, there
isn't any way to change this.  As I said, I'll see what I can do about
coming up with a workaround for this in the next build.  I might be able to
do some dynamic modifications of the "am I focusable" state for the
forms/panels depending upon whether they have any focusable child controls
or not.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Oct 1 2013 11:14 PMPermanent Link

Frederick Chin

Tim,

/*
If you're seeing a "scroll to the focused control" behavior, then no, there
isn't any way to change this.  As I said, I'll see what I can do about
coming up with a workaround for this in the next build.  I might be able to
do some dynamic modifications of the "am I focusable" state for the
forms/panels depending upon whether they have any focusable child controls
or not.
*/

Yes, that is the behaviour I am referring to. It would be nice to have the form respond to its own OnKeyDown event since it is already in the list of exposed events.

Frederick
Fri, Nov 15 2013 5:04 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frederick,

<< If I press either 1 or 2, nothing happens. I need to click either on the
main form or the panel first and then press 1 or 2 and the message is shown.
>>

I checked this out in more detail, and the issue appears to be with IE8
exclusively.  With IE9 or higher (IE HTML5 option selected in the EWB
project compiler options), or with FF/Chrome, the key down events occur as
expected without having to click on the form.

In general, IE8 has some really horrible focus/activate issues with
non-focusable forms that causes issues, so it will be nice when it is no
longer needed.

If you have any other questions, please let me know.

Tim Young
Elevate Software
www.elevatesoft.com
Image