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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically invoked Keypress

Status
Not open for further replies.

vgulielmus

Programmer
Jan 27, 2014
522
RO
Unlike other events (click or Interactivechange), if Keypress is simply invoked, it is triggered but the characters are not "typed".
But with a little trick, this can be surpassed.
In the keypress event I added a third parameter (logical); if this third parameter is .T., then I called DODEFAULT().
There are some limitations, but the need for KEYBOARD command is decreased.

Interesting, though when the textbox got the focus, this characters are lost. Hm...

PUBLIC ofrm
ofrm=CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS MyForm as Form
ADD OBJECT txt as MyText
ADD OBJECT cmd as CommandButton WITH left=150,Caption='Select any row from grid and click',Autosize=.T.
ADD OBJECT grd as grid WITH top=50
PROCEDURE init
LOCAL lni
* Preparing the grid
CREATE CURSOR cChar (cChar C(10))
FOR lni=1 TO 5
INSERT INTO cChar values (CHR(64+m.lni))​
NEXT
INSERT INTO cChar values ('Two words')
GO TOP
This.grd.RecordSource='cChar'
This.grd.column1.width=100​
ENDPROC
PROCEDURE cmd.click
LOCAL lni,lcStr
lcStr=ALLTRIM(cChar.cChar)
FOR lni=1 TO LEN(m.lcStr)
ThisForm.txt.Keypress(ASC(SUBSTR(m.lcStr,m.lni,1)),0,.T.)​
NEXT​
ENDPROC​
ENDDEFINE
*********************
* Text box class
*********************
DEFINE CLASS MyText as TextBox
PROCEDURE keypress
LPARAMETERS nKey,nShift,lraised
IF lraised
DODEFAULT(nKey,nShift)​
ENDIF​
ENDPROC​
ENDDEFINE
 
I found the solution. Just add =This.value after DODEFAULT

DEFINE CLASS MyText as TextBox
PROCEDURE keypress
LPARAMETERS nKey,nShift,lraised
IF lraised
DODEFAULT(nKey,nShift)
=This.value​
ENDIF
ENDPROC​
ENDDEFINE
 
Generally you can't invoke events by calling them, we have RAISEEVENT().

Why do you think this textbox class is better than using the KEYBOARD command? What's wrong with keyboard?

Do you want to invoke keypress in other applications?
Is there a limitation of KEYBOARD you want to avoid?

Bye, Olaf.
 
It's interesting, that read access of the value property persists the value set by keypress calls, but anyway the behavior you found seems like a bug and I don't see where it would make sense to make use of that bug. If you want to add text to a textbox, simply set .value = .value + X, or set the focus to the textbox right before or after calling KEYBOARD.

Bye, Olaf.
 
Thanks for your interest.
It was just an interesting observation.
Unlike the KEYBOARD command, this trick doesn't force the object to has the focus. On the other hand, the KEYBOARD seems to be much quicker.
.value = .value + X has effect only with character data type, and appends characters only at the end. The above has not such limitations.
Using RAISEEVENT forces the same trick.
There are many events that can be called programatically. For others, like the ACTIVATE event, RAISEEVENT must be used.

PUBLIC ofrm
ofrm=CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS MyForm as Form
ADD OBJECT txt as MyText WITH value=0
ADD OBJECT cmd as CommandButton WITH left=150,Caption='Select any row from grid and click',Autosize=.T.
ADD OBJECT grd as grid WITH top=50
PROCEDURE init
LOCAL lni
* Preparing the grid
CREATE CURSOR cChar (cChar C(10))
FOR lni=1 TO 5
INSERT INTO cChar values (CHR(48+m.lni))​
NEXT
INSERT INTO cChar values ('Two words')
GO TOP
This.grd.RecordSource='cChar'
This.grd.column1.width=100​
ENDPROC
PROCEDURE cmd.click
LOCAL lni,lcStr
lcStr=ALLTRIM(cChar.cChar)
FOR lni=1 TO LEN(m.lcStr)
ThisForm.txt.Keypress(ASC(SUBSTR(m.lcStr,m.lni,1)),0,.T.)
* RAISEEVENT(ThisForm.txt,"keypress",ASC(SUBSTR(m.lcStr,m.lni,1)),0,.T.)​
NEXT​
ENDPROC​
ENDDEFINE
*********************
* Text box class
*********************
DEFINE CLASS MyText as TextBox
PROCEDURE keypress
LPARAMETERS nKey,nShift,lraised
IF lraised
DODEFAULT(nKey,nShift)
=This.value​
ENDIF​
ENDPROC​
ENDDEFINE
 
Unlike the KEYBOARD command, this trick doesn't force the object to has the focus.

But if your aim is to add characters to a textbox, why do you need to simulate the typing of keystrokes? Why not just use the textbox's Value propery (as Olaf suggested)?

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for your interest.
Issuing ThisForm.txt.Keypress(someascii,0) on a regular textbox, triggers the keypress event but the character is not added to the value.
So I found this little trick and I thought it could be useful for others.
It wasn't about a need, but about a posibility.


My respects,
Vilhelm-Ion Praisach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top