Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear keyboard buffer from Keypress event 1

Status
Not open for further replies.

randymce

IS-IT--Management
Oct 1, 2003
26
US
I've assigned hotkeys to command buttons by using the Keypress method of the form as follows:

LPARAMETERS nKeyCode, nShiftAltCtrl
DO case
case nKeyCode == 28 && F1
thisform.btnAdd.Click()
case nKeyCode == -1 && F2
thisform.btnDel.Click()
case nKeyCode == -8 && F9
thisform.btnPrint.Click()
ENDCASE

It seems to work fine EXCEPT... if a cell in a grid control has the focus when pressing the hotkey, when the focus is returned to that cell, the text in the cell is replaced by the contents of the keyboard buffer. I tried to include a CLEAR TYPEAHEAD command after the CASE statement above, but that didn't help.

Anyone know how to remove that keystroke from the buffer? Or any ofther good ideas?
 
Could you explain this behaviour in a little more detail. Right now, I can't see what characters would be in the keyboard buffer in the case you described.

If the user has focus in a grid cell, then he hits, say, F9, the only key in the buffer would be F9. This is immdediately processed by your keypress routine, which means that the buffer will now be empty. You say the "text in the cell is replaced by the contents of the keyboard buffer". So, what are you actually seeing in cell?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
You should use ON LEY LABEL instead. You cannot invoke a method with ON KEY LABEL directly, but you can do it indirectly with a lead program than calls the form.

*Main PRG
ON KEY LABEL F1 HotKeyF1()
ON KEY LABEL F2 HotKeyF2()
ON KEY LABEL F9 HotKeyF9()
DO form ('YourForm.scx') NAME MainForm
* Remember to Clear Events in the Forms Destroy Method.
READ EVENTS
ON KEY

*============================
PROCEDURE HotKeyF1
MainForm.btnAdd.Click()
ENDPROC
PROCEDURE HotKeyF2
MainForm.btnDel.Click()
ENDPROC
PROCEDURE HotKeyF9
MainForm.btnPrint.Click()
ENDPROC

Tony

 
You should add NODEFAULT in the Keypress, if you want to clear the key pressed.

Bye, Olaf.
 
* Remember to Clear Events in the Forms Destroy Method.

You can clear the key assignments by issuing an empty ON KEY LABEL for each key or by using PUSH KEY CLEAR.

Remember to put code to clear the assignments in every possible exit from the area where they're in use - including all the possible error conditions.

Personally I would avoid ON KEY LABEL and use the form's KeyPress if at all possible.

By the way, do you have the form's KeyPreview set .T. so that the form will intercept the keypress before the grid sees it?

Geoff Franklin
 
1) When pressing the F2 key, a 'Ÿ' character replaces the previously existing grid cell text. (F9 seems to work OK, F1 brings up the HELP window in the dev env but works in runtime... I'll resolve that later.)
2) Although ON KEY could work - I want to try to use the form KeyPress event, it seems to be a much more elegant solution. (Thanks 'alvechurchdata' for agreeing!)
3) Yes, KeyPreview is .T.

BINGO!! - Thanks OlafDoschke, the NODEFAULT keyword worked like a charm. (A search in the VFP Help system for 'nodefault' brought up a section titled "Method and Event Code Guidelines" which included using NODEFAULT with the KeyPress event specifically - and gave an example.)

--Randy
 
Randy,

When pressing the F2 key, a 'Ÿ' character replaces the previously existing grid cell text.


This is a long-standing bug in VFP. The workaround is to clear the Format property of the control in question.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top