shifty12
asked on
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?
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........leftleftleftl eft". 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.
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.
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Perhaps if you explain the bigger picture we can come up with a solution for you...