2

Is there any way to capture which keyboard key is pressed or which mouse button is clicked in "On Click" event of a shape? In an agent based sample, I need two different actions when user clicks on a specific shape in two different occasions. How can I handle it?

Thanks in advance

1 Answers1

0

Is there any way to capture which keyboard key is pressed or which mouse button is clicked in "On Click" event of a shape?

No, there is no way to do that built-in to AnyLogic: you must left-click all controls and there is no 'press a key' type construct in AnyLogic which would allow you to intercept any key press.

If you want multiple possible user actions, why not just have multiple buttons or, say, a Combo Box and Button?

If you want the button to do different things dependent on the current state of the model you would do that by relevant conditional code (e.g., via Java if statements) in the Button's action (and you could make the label text of the button dynamic so that the label refers to what the button will do if pressed now, which requires some Java to code the logic for the dynamic value).

What is your actual design requirement?

Stuart Rossiter
  • 2,432
  • 1
  • 17
  • 20
  • Again thanks for above answer, you made it clear that there is no such a way. But I didn't need other mouse button clicks capture on a "Button". I need such right-click captures (or a keyboard key pressed while clicking) on a "shape", as I've mentioned in question. And I think it is somehow usual in different programming tools. Anyway, my actual requirement is as follows: several shapes in model each one represent an entity, and two or three different actions needed when user clicks on each shape. – Mohammad Hasan N. Oct 19 '18 at 13:21
  • And time is important, so the idea of using a button or combo box (which unfortunately I am now forced to utilize) may result in a not good enough UI. User may need to implement different actions on different shapes rapidly and if he/she is needed to click a combo box each time (outside the main part of the model) it would be (as it is now) so slower than the case he/she can change the action only using mouse right click or a key on keyboard. Imagine, on a map, you were forced to click a certain button whenever you need right-click menu (in case no right click was supported), was it good UI? – Mohammad Hasan N. Oct 19 '18 at 13:29
  • Before AnyLogic 8.3, the AnyLogic presentation was based on Java Swing so you could access the Panels being used and add your own Swing controls (which could include stuff similar to what you are asking for). However, now AnyLogic models are Web applications with HTML5 front ends so this avenue is closed to you (though you can do the same via HTML, though I have no experience of that). – Stuart Rossiter Oct 19 '18 at 17:45
  • I've cleaned up my answer to remove the stuff about the design potentially not making sense (now I see your sensible clarification). Your 'time is important' bit almost makes it sound like you are writing a game-like simulation (which I *guess* AnyLogic could theoretically be used to write). – Stuart Rossiter Oct 19 '18 at 17:49
  • Yes, It is somehow a game like simulation but not only for fun...:) – Mohammad Hasan N. Oct 20 '18 at 17:27