Author Topic: EnterInField - ReturnInField ?  (Read 9963 times)

carlk

  • Newbie
  • *
  • Posts: 13
EnterInField - ReturnInField ?
« on: December 30, 2010, 10:44:53 PM »
Hello.  By way of introduction, I'm new to HyperNext although I've done extensive work with HyperCard.  I've created a lot of HC stacks, from very simple ones to some that are quite complex, such as one for cross-country ski race timing and a couple that do structural engineering calculations for architectural work.  I'm now discovering the capabilities and limitations of HN relative to HC and have a project that is coming along pretty nicely - a spelling word practice stack for my grade grandson who is in the 4th grade.  But it certainly seems like more work than when I did the same kinds of things in HC.  On the other hand, I'm happy to have found something very similar to HC that works on Mac OSX ... and that's free (so far).   :D

That brings me to my current question.  Is it possible to catch and respond to the Enter key and/or the Return key being pressed after typing a word into a field?  Basically, I need the equivalent capabilities of "on EnterInField" and "on ReturnInField" that HyperCard provided for.

I've experimented a bit with enabling Field events and experimenting with...
FieldEventFN
FieldKeyDownFN
KeyCharFN
... but I haven't been able to come up with anything that works yet.  I'm hoping that I'm just missing something simple here.

I can, of course, simply resort to the user having to click on a button to initiate action on the field entry - which is how it is currently working - but that's nowhere near as smooth as being able to hit a key to accomplish the task of comparing the entered word with the correctly spelled word and responding appropriately.
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 420
  • Here to help you
Re: EnterInField - ReturnInField ?
« Reply #1 on: December 31, 2010, 11:59:04 AM »
Quote from: "carlk"
Hello.  By way of introduction, I'm new to HyperNext although I've done extensive work with HyperCard.  I've created a lot of HC stacks, from very simple ones to some that are quite complex, such as one for cross-country ski race timing and a couple that do structural engineering calculations for architectural work.  I'm now discovering the capabilities and limitations of HN relative to HC and have a project that is coming along pretty nicely - a spelling word practice stack for my grade grandson who is in the 4th grade.  But it certainly seems like more work than when I did the same kinds of things in HC.  On the other hand, I'm happy to have found something very similar to HC that works on Mac OSX ... and that's free (so far).   :D

That brings me to my current question.  Is it possible to catch and respond to the Enter key and/or the Return key being pressed after typing a word into a field?  Basically, I need the equivalent capabilities of "on EnterInField" and "on ReturnInField" that HyperCard provided for.

I've experimented a bit with enabling Field events and experimenting with...
FieldEventFN
FieldKeyDownFN
KeyCharFN
... but I haven't been able to come up with anything that works yet.  I'm hoping that I'm just missing something simple here.

I can, of course, simply resort to the user having to click on a button to initiate action on the field entry - which is how it is currently working - but that's nowhere near as smooth as being able to hit a key to accomplish the task of comparing the entered word with the correctly spelled word and responding appropriately.

Hi Carlk

Welcome to our forums and its really great to hear you are a HyperCard user - not so many of us about now.
Thanks so much for the info on your background - really interesting to know and clearly you understood HyperCard very well.
I'm afraid you'll find HyperNext doesn't have all the depth of HyperCard with its hierarchical messaging etc but its still easy to use for many tasks.
Also the HyperNext's compiler isn't as sophisticated as HyperCard's so for some tasks you need more lines of code but HyperNext is still being developed and will get better.


About your field responding to a key return - here is some code:-

When your card opens you need to tell the field to respond to a keydown event because by default keydown events are switched off. Unfortunately this keyword is not mentioned in either the built-in help or Language Reference PDF.

Code: [Select]
@ enable keydown for field 2
FieldSetKeyDown(2,1)

To disable keydown events for a particular field just pass a zero to it .
Code: [Select]
@ disable keydown for field 2
FieldSetKeyDown(2,0)


In your field you can see which key was pressed with the following code:-

Code: [Select]
local evnum,keyval,keyasc

Put FieldEventFN(2) into evnum

@ check for keydown event
if evnum=7 then
    @ get character
    Put FieldKeyDownFN into keyval
    @ get ascii value
    Put AscFN(keyval) into keyasc
    if keyasc=13 then
        Message 'Return pressed'
    Endif
EndIf

I hope this helps and best wishes for  a Happy New Year.
Malkom
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

carlk

  • Newbie
  • *
  • Posts: 13
Re: EnterInField - ReturnInField ?
« Reply #2 on: December 31, 2010, 09:53:14 PM »
Thank you very much for you quick reply, Malkom.  I now have successfully implemented your hidden/undocumented feature into my program so the user word field responds to either the Return key or the Enter key being pressed, as well as to the less elegant solution of my original Enter button being clicked on.  In the process, I also learned something new.  I figured out how to create a new card procedure that is now called by each of these three possible responses, so the code for actually processing, evaluating and responding to the user input is only in that card procedure.

This means my spelling test program now does all of the basics it needs to do in order for my grandson to be able to use it.  But there are still many additions and refinements I have partially designed into it but not yet implemented, or that I have planed for it.  The programmers quest for the perfect product is never ending.  :D

And now I can use this feature when re-doing the HC stacks I created for my daughter to help her learn her math facts.  My grandson is still using those stacks but has to do so on an old iMac that's running on System 9.  It would be better to get them updated with HyperNext so they will run on OSX.  And using the Return or Enter key is more important for them because the practice sets are timed, which makes having to click on an Enter button undesirable.  The spelling practice isn't timed, so using the Return or Enter keys is more a matter of convenience and refinement.

Ahhhhhhhhhh, those hidden/undocumented features.  Your revelation of this one, of course, only begs the question ... do you have a list of any other such features you would be willing to share so we can refer to it, even if it doesn't include full explanations and examples yet?  Provided with even the barest clues, we might be able to figure out how to make use of such features on our own, or we would at least know about them and could ask for your help in implementing them.

Thanks, again, and a very Happy New Year to you!
Carl
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 420
  • Here to help you
Re: EnterInField - ReturnInField ?
« Reply #3 on: January 04, 2011, 07:01:13 AM »
Hi Carl

Its great to see you got that working and your stacks might be used to help teach your grand kids.
OS 9 - happy memories of HyperCard days.

Ah - the undocumented features - i just forgot to put them in - they'll be in new docs etc - I'll try to put up both a bugs list and an undocumented features list in these forums.

Best wishes to you and your family for the New Year :)
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

sparkythex

  • Newbie
  • *
  • Posts: 43
Re: EnterInField - ReturnInField ?
« Reply #4 on: January 05, 2011, 05:13:20 PM »
I think this is close to the same thing which is why i added it instead of doing a new one.  Hope it helps to keeps the same ideas together.
if it is totally diff., sorry.

I was looking to try to find either were the courser is at in the field, &/or if texts is selected.

I know with other IDE's you can track this still using the seltxt functions even if nothing is selected it just puts it were curser is because nothing is selected.
but either way if nothing is selected or if something is selected then it will do somthing to the selected text, example are centering or italics or bold.  (like what you would do in word or etc.)

Quote from: "Malkom"

About your field responding to a key return - here is some code:-

When your card opens you need to tell the field to respond to a keydown event because by default keydown events are switched off. Unfortunately this keyword is not mentioned in either the built-in help or Language Reference PDF.

Code: [Select]
@ enable keydown for field 2
FieldSetKeyDown(2,1)

To disable keydown events for a particular field just pass a zero to it .
Code: [Select]
@ disable keydown for field 2
FieldSetKeyDown(2,0)


In your field you can see which key was pressed with the following code:-

Code: [Select]
local evnum,keyval,keyasc

Put FieldEventFN(2) into evnum

@ check for keydown event
if evnum=7 then
    @ get character
    Put FieldKeyDownFN into keyval
    @ get ascii value
    Put AscFN(keyval) into keyasc
    if keyasc=13 then
        Message 'Return pressed'
    Endif
EndIf

I hope this helps and best wishes for  a Happy New Year.
Malkom

would something like this work for inserting?

example lets say i have text in field that says
Code: [Select]
asdfghlets say user has text selected of "df"

i read you can take
Code: [Select]
@@@Returns the text selected by the user double clicking in the specified field. For example, below specifies field 2.
   Put FieldSelTextFN(2) into txtSelected

so then if add some thing like
Code: [Select]
@@<b> & </b> (fyi that is html to make text bold.)use the
Code: [Select]
put '<b>' into CodeStart
    put txtSelected into CodeMidd
@ note i have a global var called nbsp = a space of ' '
   append nbsp onto txtall
   append CodeStart onto txtall
    append CodeMidd onto txtall
    append CodeEnd onto txtall
@@@@ here is were I'm stumped at.
@ assuming all this came from  field 3
@ i think this is part of what i need to put it back.
@ but i know the put is wrong.
put txtall into line n of field 3
or is it something like
Put txtall onto FieldSelTextFN(2)  <<<< which replaces what is selected????

how can i make it update to say
Code: [Select]
as <b>df</b> gh
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 420
  • Here to help you
Re: EnterInField - ReturnInField ?
« Reply #5 on: January 06, 2011, 08:26:55 PM »
You are on the right track but you correctly identify a problem with replacing the selected section of the text with a surrounding bold tag:-

Code: [Select]
@ +++ get selected text (event number will be 1) +++
Put FieldSelTextFN(3) into txtSelected
put txtSelected into CodeMidd

@ +++ build tag and contents +++
@ assume CodeStart, CodeEnd, nbsp already assigned
append nbsp onto txtall
append CodeStart onto txtall
append CodeMidd onto txtall
append CodeEnd onto txtall

@ +++ PROBLEM ++++
@ but replacing the correct part of the line with surrounding bold tag is problematic
put txtall into line n of field 3

When the field event returns the selected text it also returns information on the line selected and the mouse position along that line. Unfortunately there is a bug in HyperNext and sometimes incorrect position data is returned.

There is a real hack of a work-around - its messy but you could put it into a procedure until the HyperNext bug is fixed and the select text routine improved.

Basically use a canvas with the same font and fontsize as your field so you can determine the size of a single character. Then use the mouse coordinates relative to the screen and if you use a proportional font its relatively easy to calculate accurately the position of the character touched - which line, and which position along that line.

Put this code in your card's opening script:-

Code: [Select]
Global charWidth,charHeight
Local text

@ Use canavas 0 as it is always hidden
CanvasSetWidth(0,300)
CanvasSetHeight(0,300)
CanvasSetFont(0,'Courier New')
CanvasSetFontSize(0,16)

Put 'A' into text

Put CanvasTextWidthFN(0,text) into charWidth

Put CanvasTextHeightFN(0,text,1000) into charHeight


Put this code in your field's script - the one which the user double-clicks:-

Code: [Select]
Global charWidth,charHeight
Local mx,my,basex,basey,cardx,cardy,fx,fy
local charx,chary

Put SystemMouseXFN into mx
Put SystemMouseYFN into my

@ subtract menu bar and window height when running inside Creator
Subtract 50 from my

@ take into account the card's position with the screen - assume card 1
Put CardLeftFN(1) into cardx
Put CardTopFN(1) into cardy
Subtract cardx from mx
Subtract cardy from my

@ take into account the field's position within the card - assume field 3
Put FieldLeftFN(3) into fx
Put FieldTopFN(3) into fy
Subtract fx from mx
Subtract fy from my

Put DivFN(mx,charWidth)  into charx
Increment charx
Put DivFN(my,charHeight) into chary
Increment chary

The variables chary and charx are the line and character number respectively.

Once you have the location of the double clicked char - then you can use the select text value to replace it with the bold tag.
Of course the char position is just that of the char under the mouse and the selected text may be several chars long  but as you know the length in chars of the line in the field so can work out the full position and the repalce that text with the bold tagged text.
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

sparkythex

  • Newbie
  • *
  • Posts: 43
Re: EnterInField - ReturnInField ?
« Reply #6 on: January 07, 2011, 05:53:52 AM »
I am playing with this right now, I may figure it out.
I am a little confuse on the code to put text back.
I don't see were I use the
Code: [Select]
put txtall into line n of field 3I don't even see var "n" referenced.
or
should it be
Quote from: "Malkom"
then you can use the select text value to replace it with the bold tag.
Code: [Select]
Put txtall onto FieldSelTextFN(2)

Quote from: "Malkom"
Basically use a canvas with the same font and fontsize as your field so you can determine the size of a single character. Then use the mouse coordinates relative to the screen and if you use a proportional font its relatively easy to calculate accurately the position of the character touched - which line, and which position along that line.
now this section of your comment brings up something i have noticed but never cared enough to say anything.
i have noticed that when i copy text out of the internal guide or the Language Ref V383.pdf.  it shows up as a diff font size.
if i quit & reopen H.N. the recently pasted text seems to be now matching size of original default of everything else.
however, if this problem gets inherited what would happen if text is copy & pasted that is a diff size?   would that negate the font size select replace?
or would I have to do a double check?
example: let it pull both the mouse position from  the seltext & canvas then if it mismatches, either put message that it failed, or assume the canvas font would be more correct.
how much testing have you done with this bug?  
is it worth  a fail message & ask user to try to redo the selection again?
 or would they have to close & reopen?  
or is it always consistent to that particular text?
could this be the source of the bug somehow text is of diff size?
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

HCM2011

  • Newbie
  • *
  • Posts: 3
Re: EnterInField - ReturnInField ?
« Reply #7 on: June 16, 2011, 11:52:31 PM »
Good afternoon. I have activated the key down feature for my canvas but am still having issues getting it to do what I want it too. Below is a screenshot and explanation of what I'm trying to do.

When the start button is pressed the tree lights up and then the RT time starts registering.

When the enter key is pressed I want the RT to stop counting and then when the enter key is released I want to enter that time into the 1/4 mile field.

Below is the current code.

Card - CanvasSetKeyDown(1,1)
Put KeyDownFN(13) into accel

Start button - CanvasSetColor 1,255,255,0
CanvasFillOval 1,353,133,27,27
CanvasFillOval 1,445,133,27,27
Wait(1,3000)
CanvasFillOval 1,353,173,27,27
CanvasFillOval 1,445,173,27,27
Wait(1,500)
CanvasFillOval 1,353,213,27,27
CanvasFillOval 1,445,213,27,27
Wait(1,500)
CanvasSetColor 1,124,252,0
CanvasFillOval 1,353,253,27,27
CanvasFillOval 1,445,253,27,27

Label 1
Call Reaction
GotoLabel 1

Reaction proc - If accel=0 then
Wait(1,0.1)
Add 0.001 to RT
Put RT into field 1
Endif

View log button - Put field 1 into RT
Put field 3 into quarter
Divide quarter by 2
Put quarter into eigth
GotoCard 2

Any ideas?
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 420
  • Here to help you
Re: EnterInField - ReturnInField ?
« Reply #8 on: June 17, 2011, 10:48:48 AM »
Quote from: "HCM2011"
Good afternoon. I have activated the key down feature for my canvas but am still having issues getting it to do what I want it too. Below is a screenshot and explanation of what I'm trying to do.

When the start button is pressed the tree lights up and then the RT time starts registering.

When the enter key is pressed I want the RT to stop counting and then when the enter key is released I want to enter that time into the 1/4 mile field.

Below is the current code.

Card - CanvasSetKeyDown(1,1)
Put KeyDownFN(13) into accel

Start button - CanvasSetColor 1,255,255,0
CanvasFillOval 1,353,133,27,27
CanvasFillOval 1,445,133,27,27
Wait(1,3000)
CanvasFillOval 1,353,173,27,27
CanvasFillOval 1,445,173,27,27
Wait(1,500)
CanvasFillOval 1,353,213,27,27
CanvasFillOval 1,445,213,27,27
Wait(1,500)
CanvasSetColor 1,124,252,0
CanvasFillOval 1,353,253,27,27
CanvasFillOval 1,445,253,27,27

Label 1
Call Reaction
GotoLabel 1

Reaction proc - If accel=0 then
Wait(1,0.1)
Add 0.001 to RT
Put RT into field 1
Endif

View log button - Put field 1 into RT
Put field 3 into quarter
Divide quarter by 2
Put quarter into eigth
GotoCard 2

Any ideas?

A problem is that HyperNedxt doesn't respond to the keyUp event - so when you release the key you won't receive feedback.

Perhap you could use a flag(toggle) to note the state of the enter key - place this in Start Button and set it to 0
Then the next time the enter key is pressed to check the flag and if its set you know the enter key has "gone up"


Code for field:-
Code: [Select]
Global toggleFlag
Local evnt,key,cnum

Put CanvasEventFN(1) into evnt

if evnt=7 then
    @ key down
    Put CanvasKeyDownFN into key
    Put Asc(key) into cnum
    if cnum=13 then
        if toggleFlag=1 then
            @ key is down
            .....
            Reset(toggleFlag)
        else
            @ key is up
            .....
            Set(toggleFlag)
        endif
    endif
endif
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

HCM2011

  • Newbie
  • *
  • Posts: 3
Re: EnterInField - ReturnInField ?
« Reply #9 on: June 17, 2011, 01:11:14 PM »
Ok I added the code you posted and am getting an error saying unknown command. Here's the code as I have it now on the "Start" button. Ok so I got the problem with the unknow command to go away but I have an issue with invalid source in this line - Put Asc(key) into cnum


CanvasSetColor 1,255,255,0
CanvasFillOval 1,353,133,27,27
CanvasFillOval 1,445,133,27,27
Wait(1,3000)
CanvasFillOval 1,353,173,27,27
CanvasFillOval 1,445,173,27,27
Wait(1,500)
CanvasFillOval 1,353,213,27,27
CanvasFillOval 1,445,213,27,27
Wait(1,500)
CanvasSetColor 1,124,252,0
CanvasFillOval 1,353,253,27,27
CanvasFillOval 1,445,253,27,27


Local evnt,key,cnum

Put CanvasEventFN(1) into evnt

if evnt=7 then
@ key down
Put CanvasKeyDownFN into key
Put Asc(key) into cnum
if cnum=13 then
if toggleFlag=1 then
@ key is down
Reset(toggleFlag)
else
@ key is up
Set(toggleFlag)
endif
endif
endif

Label 1
Call Reaction
GotoLabel 1
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 420
  • Here to help you
Re: EnterInField - ReturnInField ?
« Reply #10 on: June 17, 2011, 03:25:47 PM »
I'm sorry thats my typing mistake - should be AscFN - as all HyperNext functions end in FN.

Code: [Select]
Put AscFN(key) into cnum
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

HCM2011

  • Newbie
  • *
  • Posts: 3
Re: EnterInField - ReturnInField ?
« Reply #11 on: June 17, 2011, 08:06:11 PM »
First off thank you for all your help so far. Below is the current code and I can get the "RT" to count but still not getting it to pick up the enter press and release.

Local delay

Put field 7 into delay

CanvasSetColor 1,255,255,0
CanvasFillOval 1,353,133,27,27
CanvasFillOval 1,445,133,27,27
Wait(1,2000)
CanvasFillOval 1,353,173,27,27
CanvasFillOval 1,445,173,27,27
Wait(1,delay)
CanvasFillOval 1,353,213,27,27
CanvasFillOval 1,445,213,27,27
Wait(1,delay)
CanvasSetColor 1,124,252,0
CanvasFillOval 1,353,253,27,27
CanvasFillOval 1,445,253,27,27

Label 1

Local evnt,key,cnum


Put CanvasEventFN(1) into evnt

if evnt=7 then
@ key down
Put CanvasKeyDownFN into key
Put AscFN(key) into cnum
if cnum=13 then
if toggleFlag=1 then
@ key is down
Reset(toggleFlag)
else
@ key is up
Set(toggleFlag)
endif
endif
endif

If toggleflag<1 then
Wait(1,0.1)
Add 0.001 to RT
Put RT into field 1
GotoLabel 1
endif
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »

tigabyte

  • Administrator
  • Newbie
  • *****
  • Posts: 54
Re: EnterInField - ReturnInField ?
« Reply #12 on: June 18, 2011, 10:40:45 AM »
Just a suggestion - it appears thay you have Wait a statement inside the Canvas event handler - this could interfere with HyperNext events.

A better way might be to use a Timer on the card to handle incrementing a counter every 0.1 second and use the earlier canvas code for detecting the Enter key presses - it works smoothly and might help with what you are trying to do.

Card:-
Code: [Select]
TimerOff(1)

CanvasClear(1,0,0,0)
CanvasSetKeyDown(1,1)



Button:-

Code: [Select]
Global toggleFlag,timerTicks

Clear toggleFlag
Clear timerTicks

TimerSet(1,2,100)


Timer:-
Code: [Select]
Global timerTicks

Increment timerTicks

Put timerTicks into field 2


Canvas:-
Code: [Select]
Global timerTicks
Local evnt,key,cnum

Put CanvasEventFN(1) into evnt

if evnt=7 then
    Beep
    @ key down
    Put CanvasKeyDownFN into key
    Put AscFN(key) into cnum
    if cnum=13 then
        if toggleFlag=0 then
            @ key is down
            Set(toggleFlag)
            TimerOff(1)
        else
            @ key is up
            Set(toggleFlag)
            Put timerTicks into field 3
        endif
    endif
endif
« Last Edit: January 01, 1970, 12:00:00 AM by Guest »