Mouse, keyboard & time

Position and key state are plain properties; press/release/move are virtual methods.

On this page · 8 entries
mousekeyboard
MouseX / MouseY / PMouseX / PMouseYKey / KeyCode / IsKeyPressed
IsMousePressed / MouseButtonCODED, BACKSPACE, TAB, ENTER, RETURN, ESC, DELETE
MousePressed() / MouseReleased() / MouseClicked()UP, DOWN, LEFT, RIGHT, ALT, CONTROL, SHIFT
MouseDragged() / MouseMoved() / MouseWheel(delta)KeyPressed() / KeyReleased()
FocusedKeyTyped()

KeyTypedvirtual

  • KeyTyped() → void

Fires for printable keys only (Key != CODED) — a companion to KeyPressed()/KeyReleased(), which fire for every key including arrows and modifiers.

Focused

  • Focused → bool

Whether the sketch's window currently has input focus. Defaults to true; a host that embeds the sketch updates it as its window gains/loses focus.

Millis · NanoTime

  • Millis() → long
  • NanoTime() → long

Millis() is milliseconds since the sketch started. NanoTime() is the same idea at nanosecond resolution — limited in practice by whatever precision the OS timer actually offers.

Day · Month · Year · Hour · Minute · Second

  • Day()
  • Month()
  • Year()
  • Hour()
  • Minute()
  • Second() → int

Reads the local system clock at call time — unlike Millis(), which is relative to sketch start.

Cursor · NoCursor

  • Cursor(CursorKind kind)
  • NoCursor()

Raises the CursorRequested event.

SelectInput · SelectOutput · SelectFolder

  • SelectInput(prompt, callback)
  • SelectOutput(prompt, callback)
  • SelectFolder(prompt, callback)

Raises the SelectFileRequested event with a SelectFileRequest describing which native dialog to show. DanaProcessing doesn't own a window, so — like Cursor()/NoCursor() — this can't pop a dialog itself; the host shows its own native dialog and calls Respond(path) (or Respond(null) on cancel) once the user answers.

Launch

  • Launch(string path)

Opens a file, folder, URL, or application with the OS's default handler, like Processing's launch(). Fire-and-forget — returns as soon as the OS has been asked to open it. Unlike SelectInput/Cursor, this doesn't need a host at all: it goes straight to Process.Start.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// Mouse and keyboard: events and the state you can read any time.
public class Input : Sketch
{
    readonly List<PVector> dots = new();
    string lastKey = "none";

    public override void Setup() => Size(600, 400);

    public override void Draw()
    {
        Background(IsMousePressed ? 60 : 30);
        Fill(255, 200, 80); NoStroke();
        foreach (var d in dots) Circle(d.X, d.Y, 12);

        Stroke(255); StrokeWeight(2);
        Line(PMouseX, PMouseY, MouseX, MouseY);   // last frame's mouse to this one's

        NoStroke(); Fill(255);
        Text($"mouse {MouseX:0},{MouseY:0} · button {MouseButton} · last key {lastKey} · time {Millis() / 1000}s", 10, 20);
        Text("click: dot · drag: many dots · C: clear · arrows: move all", 10, 390);
    }

    public override void MousePressed() => dots.Add(new PVector(MouseX, MouseY));
    public override void MouseDragged() => dots.Add(new PVector(MouseX, MouseY));

    public override void KeyPressed()
    {
        lastKey = Key == CODED ? $"code {KeyCode}" : Key.ToString();
        if (Key == 'c') dots.Clear();
        var step = KeyCode == LEFT ? new PVector(-10, 0) : KeyCode == RIGHT ? new PVector(10, 0)
     : KeyCode == UP ? new PVector(0, -10) : KeyCode == DOWN ? new PVector(0, 10) : new PVector(0, 0);
        for (int i = 0; i < dots.Count; i++) dots[i] += step;
    }
}