HOWTO insert text via function keys

Moderator: Rathinagiri

Post Reply
Ricci
Posts: 255
Joined: Thu Nov 19, 2009 2:23 pm

HOWTO insert text via function keys

Post by Ricci »

Remember the "good old" Clipper times? Using the keyboard() command you were able to insert predefined text in any GET field.
Well, times has changed and the keyboard is only one of many devices, you don´t have one type of input fields but different kinds (Textbox, Editbox, Datepicker, ...).

Here is my solution to do something like the old keyboard() command. My wish was to insert the actual date at the current cursor position by pressing the F2 key when the user is in a form. In Textbox and Editbox the date should be inserted, in Datepicker the existing value should be replaced. If the cursor (or focus) is on another control, nothing should be happen.

First we define a form with the three kinds of controls:

Code: Select all

#include "hmg.ch"
#include "hbqtgui.ch"

FUNCTION Main()

	LOCAL oForm1

	HbQt_ErrorSys()

   	DEFINE MAINWINDOW oForm1
				ROW  10
				COL  10
				WIDTH	300
				HEIGHT 200
				FONT "ARIAL"; SIZE 09

		DEFINE TEXTBOX	oTextBox1
					ROW 10; COL 10
					WIDTH 200; HEIGHT  20
					VALUE "Press F2 "
		END TEXTBOX

		DEFINE EDITBOX	oEditBox1
					ROW 40; COL 10
					WIDTH 200; HEIGHT 100
					VALUE " and here too"
		END EDITBOX

		DEFINE DATEPICKER oDatePick1
					ROW 160; COL 10
					WIDTH 90; HEIGHT 20
					VALUE CTOD("01/01/2010")
		END DATEPICKER

	END Window

	ACTIVATE WINDOW oForm1

RETURN Nil
Now we add the action for the function key before the ACTIVATE command:

Code: Select all

	...

	END Window

	ON KEY F2 of oForm1 action InsertDate( )

	ACTIVATE WINDOW oForm1

	....
Now comes the tricky part. First we have to find the active control, this is done by the ThisW() function:

Code: Select all

oWidget := HMGAPP():ThisW()
Next step is to find the type of control, here we ask for the classname:

Code: Select all

cClassname := oWidget:QTObject:className()
The rest is simple, for every class we use the specific insert or replace command.
The whole function would look like:

Code: Select all

FUNCTION InsertDate( )

	LOCAL oWidget := HMGAPP():ThisW()
	LOCAL cClassname
	
	IF hb_IsObject( oWidget )
		cClassname := oWidget:QTObject:className()
		
		DO CASE
			CASE cClassname == "QLINEEDIT"
				  oWidget:QTObject:insert( DTOC( DATE() ) )
			CASE cClassname == "QTEXTEDIT"
				  oWidget:QTObject:insertPlainText( DTOC( DATE() ) )
			CASE cClassname == "QDATEEDIT"
				  oWidget:QTObject:setDate( QDate( YEAR( DATE() ), MONTH( DATE() ), DAY( DATE() ) ) )
		ENDCASE	
	ENDIF	 		

RETURN Nil
I hope that someone find it useful.

Ricci
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: HOWTO insert text via function keys

Post by Rathinagiri »

Very nice Ricci. :)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: HOWTO insert text via function keys

Post by Pablo César »

Really great ! With enormous knowleged. Congrats !
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
l3whmg
Posts: 694
Joined: Mon Feb 23, 2009 8:46 pm
Location: Italy
Contact:

Re: HOWTO insert text via function keys

Post by l3whmg »

Hi Ricci.
Very interesting and great.

Can I give you my opinion?
A) Pay attention about ThisW() method: it based upon ThisWdg() and ThisWin().
ThisWin() return always "current/focused" form object
ThisWdg() returns NOT always "current/focused" widget object. It can returns NIL.....
ThisW() can returns ThisWdg(), but if it is NIL, it returns ThisWin().....
So, I think it's better usage of ThisWdg() instead of ThisW() and you must check if it's not NIL.

B) Your example work fine, because "ON KEY F2", AFAIK, don't take focus. Take care of this

Anyway, great! Good job

Cheers
Luigi from Italy
www.L3W.it
Post Reply