Skip to content

Clipboard API

Read and write text to the system clipboard.

Methods

set(text)

Write text to the clipboard.

javascript
await gemshell.clipboard.set('Hello World');

Parameters:

  • text - Text to copy (string)

Returns: Promise<void>

get()

Read text from the clipboard.

javascript
const text = await gemshell.clipboard.get();
console.log(text);

Returns: Promise<string> - Clipboard contents

has()

Check if the clipboard has text content.

javascript
if (await gemshell.clipboard.has()) {
  const text = await gemshell.clipboard.get();
  // Use text...
}

Returns: Promise<boolean> - true if clipboard has text

Examples

Copy Game Code

javascript
async function copyShareCode() {
  const code = generateShareCode(playerData);
  await gemshell.clipboard.set(code);
  showNotification('Code copied!');
}

Import from Clipboard

javascript
async function importFromClipboard() {
  if (await gemshell.clipboard.has()) {
    const text = await gemshell.clipboard.get();
    try {
      const data = parseShareCode(text);
      applyImportedData(data);
      return true;
    } catch (e) {
      showError('Invalid code');
      return false;
    }
  }
  return false;
}

Copy Debug Info

javascript
async function copyDebugInfo() {
  const info = [
    `Game Version: ${gameVersion}`,
    `OS: ${await gemshell.os.getName()}`,
    `Level: ${currentLevel}`,
    `Score: ${playerScore}`,
    `FPS: ${currentFPS}`
  ].join('\n');
  
  await gemshell.clipboard.set(info);
}

Copy/Paste Level Codes

javascript
async function copyLevelCode() {
  const code = btoa(JSON.stringify(levelData));
  await gemshell.clipboard.set(code);
  showMessage('Level code copied!');
}

async function pasteLevelCode() {
  const text = await gemshell.clipboard.get();
  try {
    const data = JSON.parse(atob(text));
    loadLevel(data);
  } catch {
    showError('Invalid level code');
  }
}

Share High Score

javascript
async function shareHighScore(score) {
  const message = `I scored ${score} points in MyGame! Can you beat it?`;
  await gemshell.clipboard.set(message);
  showNotification('Copied to clipboard - paste anywhere to share!');
}