234

Well, the answer seems to be YES :) Here is my implementation of Minecraft classic game in Mathematica. Let’s start with some screenshots which were taken during the construction of the final scene which will be displayed an the end of this post. Features Blocks are creatable and removable One texture per block Player automatically jumps to the obstacles ...


32

Here is an attempt to draw a 3D Bloch sphere, on which we can select a point simply clicking on it with the mouse and obtain the corresponding qubit representation. In the code I made use of Szabolcs' MaTeX package to draw the labels in LaTeX (not necessary, but better looking) and Sjoerd C. de Vries' splineCircle to draw the 3D arcs (the above functions are ...


16

Perhaps something like this?: DynamicModule[{key = ""}, EventHandler[ Dynamic[key], {"KeyDown" :> (key = CurrentValue["EventKey"])} ]]


14

EDIT: converted the use of explicit Hold wrappers to appropriate HoldAll and HoldFirst attributes of helper functions. Also added possibility to change columns together with rows, and added highlighting of rows/columns that are being moved. The following is a possible implementation, allowing to visually switch neighboring rows and columns via mouse ...


12

Both solutions are undocumented but I don't think they will go away. Menu/MenuItem are used internally for a long time and AttachedCell is a relatively new concept but many of new features use it. E.g. DateSetter[], suggestion toolbar, image tools, message stack preview, etc. ContextMenu Quite out of the box. One needs to remember to use KernelExecute for ...


8

The reason for this behaviour is that as soon as the cursor gets away from a particular vertex marker it leaves the associated EventHandler. Here's a work-around, let's associate the event handler with the whole Graph. We just need to take care of updating the proper pos. DynamicModule[{ind = 1, pos1 = {1, 0}/2, pos2 = {1, 1}/2, pos3 = {-1, 0}/2}, ...


7

There is ValueFunction, documented here. It allows detecting value changes for given symbols. For example, In[1]:= Experimental`ValueFunction[x] := Print["x changed"] In[2]:= x = 6 During evaluation of In[10]:= x changed Out[2]= 6


7

You could add a trap to Documentation`HelpLookup which is called when F1 is pressed: Unprotect[Documentation`HelpLookup]; Documentation`HelpLookup[link_String, nb_, lang_String, opts : OptionsPattern[]] := Block[{$inblock = True}, lookedup[link] = True; Documentation`HelpLookup[link, nb, opts]] /; ! TrueQ[$inblock] You can add whatever code you want....


7

Sadly, this can't be done with 100% fidelity. For example, a significant issue is that on some platforms, there is no way to directly capture keystrokes which are mapped to menu commands. The operating system automatically translates the keystrokes to the appropriate menu commands long before any user-accessible code has any hope of seeing what transpired. ...


6

The following seems to be a little faster, most likely because it avoids AppendTo. DynamicModule[{list = {}}, EventHandler[ Dynamic @ Framed @ Graphics[{Red, list}, PlotRange -> 2], {"MouseDragged" :> (list = Flatten @ {list, Point @ MousePosition["Graphics"]})}]] However, you might find it more to your liking to use lines rather than ...


6

I'm not sure how to increase the mouse polling rate in Dynamic-things, but I think it has been doing really well on my computer, which is Windows 8.1 with a regular USB mouse. Here is a simple code to measure the average polling rate of Dynamic: Module[{mplst = {}, time = 10}, PrintTemporary[DynamicWrapper["Measuring...", mplst =...


6

Yes, it is possible. Have you read the tutorials about Dynamic? Here is a simple example of a continuously updated time which changes color on Mouseover Dynamic[Refresh[ Mouseover[DateString[],Style[DateString[],20,Blue]], UpdateInterval->1]] Here the slightly more complex example with two buttons. DynamicModule[{bold=False,blue=False}, Column[{ ...


6

To answer the second part of your question, you can use CurrentValue["EventKey"] to get the current key that is being pressed. Modifying your example above: EventHandler[InputField[], "KeyDown" :> Print[CurrentValue["EventKey"]]]


6

rulf[x_] := Function[{y}, If[EvenQ[x - y], Style[y, Red, Bold], y]] DynamicModule[{rul = Identity}, Column[EventHandler[Dynamic[rul@#], {"MouseMoved" :> (rul = rulf@#)}] & /@ Range@5]


6

The last example in the Classify can be used as a start. lowriniak has translated the code from the cell in the example in the answer here. The example uses logistic regression but the classifier can be changed to a neural network easily: lenet = NetModel["LeNet Trained on MNIST Data"]; (*Inputs for the canvas and brush size*) With[ {xsize = 64, ysize = ...


5

You want to create an independent DynamicModule for each EventHandler. Otherwise, the single variable col will control the color for all of the disks, as you're witnessing. Your code can be rearranged as follows: radius = 1; numDisks = 3; Graphics[Table[ DynamicModule[{col = Red}, EventHandler[{Dynamic[col], Disk[{2.5*(i - 1), 0}, ...


5

Edit: Updated to register any key presses Whenever an EventHandler does not handle something continuously, one can set up a listener manually using scheduled tasks. Here, the task task is initiated when the InputField is displayed, that scans the state of the Shift key. You can put whatever code you want to be evaluated on keypress in the update function. ...


5

Check what The Futz just said. However, you can go the ugly workaround way. Not recommended, since I don't think you have any guarantees that your code will be evaluated exactly once only when you click the button, but up to you. Try this Print@"I dare you to move the slider after pressing the button"; \ Slider[] EventHandler[Framed["Benjamin Button"], "...


5

Your code seems to intend to do something a little different than what I understood from the question - so I hope this is right. :) Fixing the code require two changes, remove Deploy -- it makes the EventHandler inoperative; and put Dynamic around the argument pt -- one of the important attributes of Dynamic is that it is HoldFirst, which allows the symbol ...


5

EventHandler can be used to catch various mouse events (mouse up, mouse down, mouse clicked, mouse dragged). Use MousePosition to add some intelligence. Example: DynamicModule[{col1 = Green, col2 = Blue}, Graphics[ { EventHandler[ Dynamic[{col1, Disk[]}, ImageSize -> Tiny], {"MouseClicked" :> (col1 = col1 /. {Red -> ...


5

Just to provide a full answer (and thanks Kuba's answer) in case someone wants to screencast while using Mathematica and byzanz (Gnome only): recordGif::Usage = "Record Mathematica animations; Syntax: recordGif[time,delay]"; recordGif[time_, delay_] := DynamicModule[{pathToGif, pathToSh, toGIF, posGIF}, Panel@Column[{ Button["Run", Print["Starting ...


5

Tell me if that's what you are looking for. DynamicModule[{kk = {3 <-> 2, 1 <-> 2, 3 <-> 1}, col}, col[1] = Green; col[2] = Red; col[3] = Blue; Dynamic@Graph[{1, 2, 3}, EventHandler[#, {"MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, (col[#2] = col[#2] /. {Red -> Green, Green -> Red, Blue -> ...


5

Kuba's answer is brilliant, but it has a rather ugly bit in it with a lot of unnecessary fooling around with Hold and ReleaseHold in order to make assignment to variables. As is so often the case with Mathematica, this fooling around with variables can be eliminated by working with a list, so I have reworked Kuba's answer to keep the vertex coordinates in a ...


5

It is not an EventHandler thing.... You can think of Dynamic as a placeholder. Whenever this Dynamic triggers, the current visual tree from that point down is replaced with a completely new one. When that happens, it is not always possible, desirable, etc... to restore the selection back to an equivalent position. You can see this for yourself. Evaluate ...


4

For version 9 or above, one can use CellPrint (or simply Print, as it is a specific case of CellPrint) which does not move the selection and thus preserves the focus on the InputField. EventHandler[InputField["", String], {"ReturnKeyDown" :> CellPrint[Cell["test", "Output"]]} ] Since the above code only works in v9, here is an equivalent with ...


4

pos = {}; SetOptions[EvaluationNotebook[], NotebookEventActions :> { "MouseClicked" :> If[CurrentValue["AltKey"], AppendTo[pos, MousePosition[]]], PassEventsDown -> True}] Dynamic@MousePosition[] Dynamic@pos So, while pressing Alt you can click to store your positions.


4

TL;DR In order to understand why what you did doesn't work you have to read up on how Mathematica stores information internally (DownValues and OwnValues), how and when Mathematica evaluates expressions (including how attributes affects that). There's a code halfway through that will do what you want. You asked "would somebody please help and point what is ...


4

plot = Plot[x^2, {x, 0, 10}, ImageSize -> 300, PlotLabel -> "Original"]; func[point_] := Total[point]; Row[{EventHandler[ plot, {"MouseClicked" :> (sp = MousePosition["Graphics"])}], Dynamic[Plot[func[sp]*t, {t, 0, Tr@sp}, ImageSize -> 300, PlotLabel -> sp]]}] Dynamic[MousePosition["Graphics"]]


4

Shortly, use a SelectorPane and hide EventHandler as soon as the condition is met: pt = {0, .05}; pane = "dynamic"; graphics = Framed[Graphics[ {{Red, Disk[Dynamic[pt], .1]}}, PlotRange -> 1, Axes -> True] ]; eh = EventHandler[#, { "LeftArrowKeyDown" :> (pt -= {0.05, 0}; check@pt), "RightArrowKeyDown" :> (pt += {0.05, 0}; ...


4

Edit: more efficient and more flexible solution: ListStepPlot[ {{1, 2, 3}, {2, 3, 5}}, BaseStyle -> Thick ] /. {styles__, l_Line} :> DynamicModule[ {edge = Thin} , {styles , Dynamic @ edge , MouseAppearance[ EventHandler[ l , "MouseClicked" :> (edge = edge /. {Thick -> Thin, Thin -> Thick} ...


Only top voted, non community-wiki answers of a minimum length are eligible