Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Why don’t arrow key presses trigger the KeyUp procedure of a custom control


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: May 26, 2017 2:16 PM Last Post By: Remy Lebeau (Te...
Enquiring Mind

Posts: 88
Registered: 10/6/08
Why don’t arrow key presses trigger the KeyUp procedure of a custom control  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 4:42 AM
Hi,

I am writing a custom edit control, TMyEdit, which is intended to exhibit similar behaviour to a TEdit control, but without using TEdit or the native Windows edit control. Its KeyUp method is programmed to respond to left and right arrow key presses by moving the cursor in the edit string backwards or forwards accordingly.

When I put just one instance of the TMyEdit control on an otherwise empty form, it responds to left and right arrow key presses as intended.

But when I put several instances of the TMyEdit control on a form already populated with several other controls, including standard TEdit controls and TButton controls, the left and right arrow key presses cause focus to be shifted to another control in the tab order, unless a TEdit control is in focus, in which case they cause the cursor in the focused TEdit control to move backwards or forwards. In other words, the arrow keys act like the tab keys to navigate between the TWinControl controls in the form. This is confirmed by the fact that if I place a breakpoint in the KeyUp procedure, execution does not stop at it when an arrow key is pressed.

The custom controls have TabStop set to True.

Can anyone suggest how to fix this problem?

TIA,
EM
Carl-Henrik Nil...

Posts: 53
Registered: 3/26/02
Re: Why don’t arrow key presses trigger the KeyUp procedure of a custom control  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 6:35 AM   in response to: Enquiring Mind in response to: Enquiring Mind
Enquiring Mind wrote:
Hi,

I am writing a custom edit control, TMyEdit, which is intended to exhibit similar behaviour to a TEdit control, but without using TEdit or the native Windows edit control. Its KeyUp method is programmed to respond to left and right arrow key presses by moving the cursor in the edit string backwards or forwards accordingly.

When I put just one instance of the TMyEdit control on an otherwise empty form, it responds to left and right arrow key presses as intended.

But when I put several instances of the TMyEdit control on a form already populated with several other controls, including standard TEdit controls and TButton controls, the left and right arrow key presses cause focus to be shifted to another control in the tab order, unless a TEdit control is in focus, in which case they cause the cursor in the focused TEdit control to move backwards or forwards. In other words, the arrow keys act like the tab keys to navigate between the TWinControl controls in the form. This is confirmed by the fact that if I place a breakpoint in the KeyUp procedure, execution does not stop at it when an arrow key is pressed.

The custom controls have TabStop set to True.

Can anyone suggest how to fix this problem?

TIA,
EM

You need to process the WM_GETDLGCODE message.
See msdn documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms645425(v=vs.85).aspx

//Declaration:
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
 
//Implementation:
procedure TMyEdit.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
  Message.Result := DLGC_WANTARROWS;
end;


--
C-H
Enquiring Mind

Posts: 88
Registered: 10/6/08
Re: Why don’t arrow key presses trigger the KeyUp procedure of a custom control  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 7:33 AM   in response to: Carl-Henrik Nil... in response to: Carl-Henrik Nil...
Carl-Henrik,

Many thanks for the answer! I have yet to look into why your suggested code works, but I have added it to my control and it solves the problem!

Another minor question that you might be able to help with. To aid in debugging I have included a Beep statement at the beginning of the TMyEdit.KeyUp procedure. I have noticed that if I press an alphanumeric key on its own, then I hear a single beep. But if I press an alphanumeric key at the same time as the shift key, then I hear two beeps in rapid succession. But if I press the shift key on its own, I hear no beeps. The two beeps I hear if I press an alphanumeric key together with the shift key are further apart in time than I would have expected.

So to investigate this behaviour further, I placed a breakpoint on the statement following the Beep statement, and pressed an alphanumeric key together with the shift key again. This time I heard a single beep and then the execution stopped at the breakpoint. When I resumed the execution from the breakpoint there was no further beep. So by stopping execution at the breakpoint I hear only one beep. Finally I ran the application EXE file outside of the IDE, in case the debugger has something to do with the double beep sound, and this time I again heard a double beep. I have checked that there are no other Beep statements in the unit containing the custom component or the form in which it is instantiated that could be causing the second beep.

Regards,
EM

Carl-Henrik Nilsson wrote:

You need to process the WM_GETDLGCODE message.
See msdn documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms645425(v=vs.85).aspx

//Declaration:
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
 
//Implementation:
procedure TMyEdit.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
  Message.Result := DLGC_WANTARROWS;
end;


--
C-H
Carl-Henrik Nil...

Posts: 53
Registered: 3/26/02
Re: Why don’t arrow key presses trigger the KeyUp procedure of a custom control  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 8:34 AM   in response to: Enquiring Mind in response to: Enquiring Mind
Enquiring Mind wrote:
I have yet to look into why your suggested code works, but I have added it to my control and it solves the problem!

Well, as you've already noticed, the default behaviour of the arrow keys differs from
that of ordinary keys. Windows offers a means of letting you dictate the behaviour instead
by handling the WMGetDlgCode (returning DLGC_WANTARROWS as Result.)
You can do similar things with the Tab key and DLGC_WANTTAB.

Another minor question that you might be able to help with. To aid in debugging I have included a Beep statement at the beginning of the TMyEdit.KeyUp procedure. I have noticed that if I press an alphanumeric key on its own, then I hear a single beep. But if I press an alphanumeric key at the same time as the shift key, then I hear two beeps in rapid succession. But if I press the shift key on its own, I hear no beeps. The two beeps I hear if I press an alphanumeric key together with the shift key are further apart in time than I would have expected.

So to investigate this behaviour further, I placed a breakpoint on the statement following the Beep statement, and pressed an alphanumeric key together with the shift key again. This time I heard a single beep and then the execution stopped at the breakpoint. When I resumed the execution from the breakpoint there was no further beep. So by stopping execution at the breakpoint I hear only one beep. Finally I ran the application EXE file outside of the IDE, in case the debugger has something to do with the double beep sound, and this time I again heard a double beep. I have checked that there are no other Beep statements in the unit containing the custom component or the form in which it is instantiated that could be causing the second beep.

I think you'll figure it out eventually, but remember that sounds are slow and windows may also
trigger a beep on it's own. I would use text strings logging (OutputDebugString) instead of beeps.
--
C-H
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Why don’t arrow key presses trigger the KeyUp procedure of a custom control  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 2:16 PM   in response to: Carl-Henrik Nil... in response to: Carl-Henrik Nil...
Carl-Henrik Nilsson wrote:

I would use text strings logging (OutputDebugString) instead of beeps.

Agreed. You can view the log messages in the IDE's "Messages" window
when running your code inside the debugger, and view the log messages
using SysInternals DebugView
(https://technet.microsoft.com/en-us/sysinternals/debugview) when
running your code outside of the debugger.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02