Skip to content
CrossFly is 100% open sourceFollow us on GitHub
CrossFlyCrossFly
Get started

This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.

How to vibrate, emit a beep and use the clipboard

Purpose. Give quick feedback to the user: vibrate the device, play a beep, and copy/paste text on the clipboard.

APIs used

  • Device.vibrate(int millis) — vibrates for millis ms. On iOS the duration is ignored (there is no way to set the time on that platform).
  • Device.beep() — plays the preloaded beep.mp3 sound. Does not block (schedules the play on the render thread).
  • Device.clipboardCopy(String s) — copies the string to the clipboard.
  • Device.clipboardPaste() → String — returns the last clipboard string, or "" if empty.

Note. All are static methods of Device. They are safe to call from any thread (beep() internally schedules on the render thread). clipboardPaste() never returns null — it returns an empty string when there is nothing.

Complete example (copy, paste and run)

package com.exemplo;

import dev.crossfly.ui.*;                   // Window, Button, Label
import dev.crossfly.ui.layout.*;            // VerticalLayout
import dev.crossfly.device.*;               // Device

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      Label resultado = new Label("");
      add(LAY_AUTO, resultado);

      Button acao = new Button("Vibrar, beep e copiar");
      acao.onAction(c ->
      {
         // Haptic and audio feedback (static methods, safe from any thread)
         Device.vibrate(50);       // 50ms (ignored on iOS)
         Device.beep();            // does not block

         // Clipboard
         Device.clipboardCopy("Texto para copiar");
         String colado = Device.clipboardPaste();   // "" if the clipboard is empty

         resultado.setText("Clipboard: " + colado);
      });
      add(LAY_AUTO, acao);
   }
}

The snippet below shows only the core. To run it, place it inside the MainWindow skeleton above.

Minimal snippet

// verified at source: Device.java (vibrate line 253, beep line 306, clipboardCopy line 271, clipboardPaste line 279)
import dev.crossfly.device.*;              // Device

// Haptic and audio feedback
Device.vibrate(50);       // 50ms (ignored on iOS)
Device.beep();            // does not block

// Clipboard
Device.clipboardCopy("Texto para copiar");
String colado = Device.clipboardPaste();   // "" if the clipboard is empty

Dive deeper

Was this page helpful?