4
$\begingroup$

I'm trying to make an interface that (basically) records what mouse button was pressed inside the cell of a matrix.

The code I have so far is

A = ConstantArray[0, {10, 10}];

DynamicModule[{pt = {0, 0}}, Dynamic@EventHandler[ArrayPlot[A],
   "MouseDown" :> (
     A[[
        Ceiling[10 - MousePosition["Graphics"][[2]]],
        Ceiling[MousePosition["Graphics"][[1]]]
      ]] = CurrentValue["MouseButtons"])]]

It kind of works, except that CurrentValue["MouseButtons"] seems to be returning strange things: it returns {1} for my left mouse button, empty set {} for my right mouse button, and nothing happens when I click my scrollwheel/middle mouse button. Why is that? Shouldn't I be getting something like 1 for left mouse, 2 for right mouse, and 3 for middle mouse buttons (at least according to the documentation)? My mouse seems to be working fine.

Thanks for any help!

$\endgroup$
3
$\begingroup$

Update: A more streamlined version of the original post:

DynamicModule[{pt = {0, 0}}, 
 Dynamic@EventHandler[ ArrayPlot[A, ImageSize -> 300, DataReversed -> True],
   {"MouseDown", #} :> (A[[##&@@ (Ceiling/@Reverse[MousePosition["Graphics"]])]] = #)&/@
    {1, 2}]]

Original post:

A = ConstantArray[0, {10, 10}];
DynamicModule[{pt = {0, 0}},  Dynamic@EventHandler[
   Row[{ArrayPlot[A, ImageSize -> 300],  Style[MatrixForm[A], 16]}],
     {{"MouseDown",  1} :> (A[[Ceiling[10 - MousePosition["Graphics"][[2]]], 
         Ceiling[MousePosition["Graphics"][[1]]]]] = 1), 
      {"MouseDown",  2} :> (A[[Ceiling[10 - MousePosition["Graphics"][[2]]], 
         Ceiling[MousePosition["Graphics"][[1]]]]] = 2)}]]

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ I can't get the middle mouse button detected with EventHandler (Version 9 Windows 10). $\endgroup$ – kglr Apr 22 '16 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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