Form keypress conflict with CmdButns in VB6

Posted on 2007-10-17
Medium Priority
Last Modified: 2013-12-26
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?
Question by:shifty12
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20093942
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...
LVL 31

Expert Comment

ID: 20095407
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.

Author Comment

ID: 20101158
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?
LVL 31

Accepted Solution

Frosty555 earned 200 total points
ID: 20111302
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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

850 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