TransWikia.com

How to get what mouse button was pressed in EventHandler?

Mathematica Asked on October 6, 2020

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!

One Answer

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

Answered by kglr on October 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP