Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: EventHandler problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88966] Re: EventHandler problem
  • From: m.r at inbox.ru
  • Date: Thu, 22 May 2008 02:38:50 -0400 (EDT)
  • References: <g03v5a$bp4$1@smc.vnet.net>

On May 10, 5:58 am, "J. McKenzie Alexander" <ja... at lse.ac.uk> wrote:
> I'm trying to design a custom InputField that indicates whether or not  =

> the user has pressed Return to submit the changes. It's a simple  
> design: a red or green disk is drawn to the right of the InputField to  =

> indicate the state.
>
> Here's the definition of the disks:
>
>         red = Graphics[{Green, AbsolutePointSize[12], Point[{0, =
0}]},
>                  ImageSize -> {20, Automatic}]
>
>         green = Graphics[{Green, AbsolutePointSize[12], Point[{0=
, 0}]},
>                  ImageSize -> {20, Automatic}]
>
> Here's my first attempt at creating the InputField:
>
>         DynamicModule[{k = 1, changed = False},
>          Row[{
>            EventHandler[
>             InputField[Dynamic[k], Number, FieldSize -> Tiny,
>              ContinuousAction -> True],
>             {"KeyDown" :> (changed = True),
>              "ReturnKeyDown" :> (changed = False)
>              },
>             PassEventsDown -> True
>             ],
>            Dynamic[If[changed, red, green]]
>            }]
>          ]
>
> Everything works except setting the value of changed back to False  
> when the Return key is pressed. Mathematica fires the action for  
> *both* "KeyDown" and "ReturnKeyDown", and the "KeyDown" action is  
> invoked after the "ReturnKeyDown" action. (This happens regardless of  
> whether one uses {"KeyDown" :> (...), "ReturnKeyDown" :> (...) } or  
> {"ReturnKeyDown" :> (...) "KeyDown" :> (...)} in the argument to  
> EventHandler.)
>
> I've tried detecting the value of the key by using  CurrentValue["EventK=
ey"] but this doesn't seem to do anything...
>
> Any thoughts on how to solve this problem would be greatly appreciated!
>
> Cheers,
>
> Jason
>
> --
> Dr J. McKenzie Alexander
> Department of Philosophy, Logic and Scientific Method
> London School of Economics and Political Science
> Houghton Street, London WC2A 2AE
>

The CurrentValue["EventKey"] behaviour is a bug. Here's a workaround:

DynamicModule[{col = Green, x = Null, ret = ""},
 EventHandler[
  {InputField[Dynamic[x]],
   Style["\[FilledCircle]", FontColor -> Dynamic[col]]},
  {"KeyDown" :> (If[! ret, col = Red]; ret = False),
   "ReturnKeyDown" :> (col = Green; ret = True)},
  PassEventsDown -> True]
 ]

Maxim Rytin
m.r at inbox.ru


  • Prev by Date: Re: On which OS is Mathematica best implemented?
  • Next by Date: Re: sending programatically output to clipboard
  • Previous by thread: EventHandler problem
  • Next by thread: Re: Bug in Plot?