Page 1 of 2

Assign a button control to arrow key

PostPosted: Sat Mar 31, 2007 1:49 am
by Jeff Barnes
Hi Everybody,

Is there a way to assign the function of a button to an arrow key so when the user uses the left or right arrow key it functions as if they clicked a button?


Thanks,

Jeff

PostPosted: Sat Mar 31, 2007 9:46 am
by Antonio Linares
Jeff,

If the button has the focus, then you may use oBtn:bKeyChar or oBtn:bKeyDown to detect those keystrokes and fire the button action

PostPosted: Sat Mar 31, 2007 8:15 pm
by Jeff Barnes
Antonio,

Actually I have 4 buttons that I would like to give the user the option to either click a button or an arrow key.

Ex. Left Arrow = BUTTON1
Right Arrow = BUTTON2
Up Arrow = BUTTON3
Down Arrow = BUTTON4


Jeff

PostPosted: Sat Mar 31, 2007 8:47 pm
by Antonio Linares
Jeff,

Which control has the focus ? The keystrokes will go to the focused control

Any of the buttons ? Or other control ?

PostPosted: Sat Mar 31, 2007 9:11 pm
by Jeff Barnes
The area where the buttons are has the focus.

Jeff

PostPosted: Sat Mar 31, 2007 9:15 pm
by Antonio Linares
Is that area a dialogbox ? a window ?

PostPosted: Sun Apr 01, 2007 12:10 am
by Jeff Barnes
Sorry, I guess I should have mentioned that.

It's a Window.


I've tried:

SETKEY( K_UP, { || MsgInfo( "UP pressed" ) } )
SETKEY( K_DOWN, { || MsgInfo( "DOWN pressed" ) } )
SETKEY( K_LEFT, { || MsgInfo( "LEFT pressed" ) } )
SETKEY( K_RIGHT, { || MsgInfo( "RIGHT pressed" ) } )

But with no luck ... basically nothing happens. (inkey.ch is in my code)


Jeff

PostPosted: Sun Apr 01, 2007 5:54 am
by Antonio Linares
Jeff,

When a window or a dialogbox has several controls, one of them has the focus. So you should assign bKeyDown or bKeyChar to all of them:

oBtn1:bKeyChar = { | nKey | MsgInfo( nKey ) }
oBtn2:bKeyChar = { | nKey | MsgInfo( nKey ) }
oBtn3:bKeyChar = { | nKey | MsgInfo( nKey ) }
oBtn4:bKeyChar = { | nKey | MsgInfo( nKey ) }

Instead of MsgInfo(), call a function where you check nKey and act accordingly

PostPosted: Sun Apr 01, 2007 12:06 pm
by Jeff Barnes
Antonio,

It seems to work with all the keys except the arrow keys :cry:

When I press an arrow key the focus moves to the next control.


Jeff

PostPosted: Sun Apr 01, 2007 2:58 pm
by Antonio Linares
Jeff,

Use bKeyDown instead of bKeyChar and try it again

PostPosted: Sun Apr 01, 2007 3:01 pm
by Jeff Barnes
Antonio,

I've tried both ... and still the arrow keys just move to the next control.



Jeff

PostPosted: Sun Apr 01, 2007 3:11 pm
by Antonio Linares
Jeff,

Please try this too besides the bKeyChar/bKeyDown:

oBtn1:nDlgCode = DLGC_WANTALLKEYS
oBtn2:nDlgCode = DLGC_WANTALLKEYS
oBtn3:nDlgCode = DLGC_WANTALLKEYS
oBtn4:nDlgCode = DLGC_WANTALLKEYS

PostPosted: Sun Apr 01, 2007 8:14 pm
by Jeff Barnes
Antonio,

Still no luck. Again, the focus is moved to the next control.


Jeff

PostPosted: Sun Apr 01, 2007 9:04 pm
by Antonio Linares
Jeff,

We are going to build a sample and try to get it working

PostPosted: Sun Apr 01, 2007 11:24 pm
by Jeff Barnes
Thanks Antonio.