Form keypress conflict with CmdButns in VB6

To display keyboard scan codes, with form_keypreview=true, I display form keypress/up/down values which works fine. If I simply add 2 no-code CmdButns to the form, all keys still display values except the 4 arrow keys, which cause toggling between the 2 CmdButns. Hello?
Who is Participating?
When you put buttons (or any control that can have focus) on the form, the arrow keys are used to move the focus from one control to the next. The event is never passed to the form because it is handled at a lower level. You'll notice also that hitting Tab does not cause a keydown event to trigger, because similarly, Tab moves the focus.

So you either need to restrict yourself to non-focus-grabbing controls like pictureboxes, or trap the keydown event at a lower level.

An alternative to pictureboxes may be a Toolbar control, this is part of the Microsoft Windows Common Controls 3.0 activex library. It is a range of buttons that can have pictures or captions, and it doesn't maintain focus like normal command buttons.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Want more fun?...add a TextBox to your form.  Now put focus on a button.  The arrows will navigate between controls and NOT fire a KeyDown() event when the BUTTONS have focus.  BUT...when the TextBox gets focus, the arrows will again fire your Forms KeyDown() event.  ;)

Perhaps if you explain the bigger picture we can come up with a solution for you...
It sounds like you're making a game. Keep in mind the keydown event has another issue, and that is that you are at the mercy of your keyboard's auto repeat rate. E.g. when you hold down the left arrow key, the form will receive "Left........leftleftleftleft". That delay is often very troublesome.

Form key_down events have always been a pain in VB. They are only truly reliable if you have nothing but non-focus controls on the form (e.g. pictureboxes or shapes). You *could* use a picturebox instead of a button, and use the Picture1_OnClick() event from there. That would be a quick fix to your problem.

However, there are a number of other (superior) ways to detect and handle keystrokes in a VB game. You game ought to have some kind of a game loop, whether it be a timer, or a tight DO...WHILE loop somewhere. The typical way to detect keystrokes is to keep track of several boolean flags, each corresponding to the state of your relevant keys. Have your main game loop call the WinAPI function GetAsyncKeyState() and determine the current state of the keys you are interested in, and set those boolean flags. Then later in your loop when you need to know the state of a key, you just check the flags.

But again, we need more context.
shifty12Author Commented:
I don't code games. I have several progs, one of which is a hex editor, that require trapping the 4 arrow keys, which, predictably, move the cursor up/down/left/right. I have had to use 4 picboxes with arrow icons to accomplish this seemingly mundane task. One should not have to call api funcs, dlls, and crude pics of arrows to facilitate key presses. The rest of the keys are trappable, whats so special about the 4 arrow keys?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.