Form keypress conflict with CmdButns in VB6

Posted on 2007-10-17
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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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

    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

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VBA: Do Loop with Input Box 14 38
    Public Sub, Empty Cell 7 52
    VBA error replacing data 6 29
    MS Date Picker 64 bit 32 bit issue 12 9
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now