7
$\begingroup$

With the concept of user responsiveness in mind, I’ve tinkered a couple of small apps that are supposed to be eh… responsive, i.e. calculating without having to press enter.
They all rely on InputField with ContinuousAction and shall only accept numbers and decimal points.

Code snippet as an example:

field1 = 1;
Grid[{{"Input a number"}, {InputField[Dynamic[field1, (field1 = #) &],
     Number, ContinuousAction -> True]}}]

Everything works except when trying to write 1.01 since it halts at the lonely zero which isn’t considered a Number. enter image description here Question:
So my question is, how to make InputField accept a lonely zero after the decimal point as a number while continually evaluating the users every keystroke.

Edit: The final goal is to get a quick and responsive CDF. I believe true ContinuousAction will open up for a lot of small useful applications.

$\endgroup$
13
  • $\begingroup$ There is somewhere closely related question. Current behaviour is quite poor. Specially in .CDF context when only Number fields are allowed. If you are not going to put it in CDF I'd work with strings $\endgroup$
    – Kuba
    Commented Feb 1, 2015 at 13:57
  • $\begingroup$ @Kuba Yes I asked a compound question where this problem was mentioned. But since I haven't solved it yet, I tried to chisel out a new and hopefully clearer question :) $\endgroup$
    – MathLind
    Commented Feb 1, 2015 at 13:59
  • $\begingroup$ @Kuba Any trick up your sleeve as to achieve the same effect? I have these old programs I wrote in Flash ActionScript that are extremely responsive. I would very much like to rewrite them in Mathematica. $\endgroup$
    – MathLind
    Commented Feb 1, 2015 at 15:08
  • $\begingroup$ Why not use Expression instead of Number. While you could use String you will need to convert to be able to use the string number. $\endgroup$ Commented Feb 1, 2015 at 22:04
  • 2
    $\begingroup$ The problem is that with ContinuousAction -> True is that the input 1.0 is immediately converted to a number and displayed, the number being the real number one. The number one is displayed automatically as 1., which removes the 0 from the InputField. I seriously doubt there is a way to hack InputField in a way that it will work in CDF (or that there is any way at all). - [Should this be an answer?] $\endgroup$
    – Michael E2
    Commented May 4, 2015 at 10:41

2 Answers 2

2
$\begingroup$

From my comment:

The problem is that with ContinuousAction -> True is that the input 1.0 is immediately converted to a number and displayed, the number being the real number one. The number one is displayed automatically as 1., which removes the 0 from the InputField. I seriously doubt there is a way to hack InputField in a way that it will work in CDF. One needs access to the input string (or an equivalent), but only numbers are disallowed; see https://www.wolfram.com/cdf/faq/details-for-mathematica-programmers.html.

$\endgroup$
2
  • 1
    $\begingroup$ And in FreePlayer only Numeric number field is available. :) $\endgroup$
    – Kuba
    Commented Oct 31, 2015 at 14:56
  • 1
    $\begingroup$ @Kuba Thanks. I added a link to a (current) reference for the limitation. $\endgroup$
    – Michael E2
    Commented Oct 31, 2015 at 15:22
0
$\begingroup$

Does this do what you need?

Grid[{{"Input a number"}, {InputField[
    Dynamic[field1, If[NumberQ[#], field1 = #, field1 = Null] &], 
    Expression, ContinuousAction -> True]}}]
$\endgroup$
8
  • $\begingroup$ @Kuba Why? .... $\endgroup$ Commented Feb 1, 2015 at 22:12
  • $\begingroup$ @Kuba the OP makes no mention of CDF $\endgroup$ Commented Feb 1, 2015 at 22:15
  • $\begingroup$ Ahh, there was a comment from OP that the final goal is CDF, I don't know why he deleted it. $\endgroup$
    – Kuba
    Commented Feb 1, 2015 at 22:15
  • $\begingroup$ @Kuba ok, well I'll leave the question for now and delete it if he confirms he wants to deploy in CDF $\endgroup$ Commented Feb 1, 2015 at 22:16
  • $\begingroup$ @Kuba actually I'll be away the rest of the week so will make it a community wiki then anyone can delete ...right? $\endgroup$ Commented Feb 1, 2015 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.