Skip to content
The Chat Archive
qwen-max-2025-01-25· Alibaba (Qwen) LMArena Human Preference 140K

Programm a boockmarklet that adds a draggable (but also lockable) controller for all…

  1. User

    Programm a boockmarklet that adds a draggable (but also lockable) controller for all arrow keys, as well as space bar and shift. It has to work with touchscreen and multi point input. It should give the underlying website the pressed buttons as keyboard inputs.

  2. Assistant

    Creating a bookmarklet that adds a draggable and lockable controller for arrow keys, space bar, and shift, while also supporting touchscreen and multi-point input, is a complex task. Below is a step-by-step explanation of how to achieve this, followed by the complete bookmarklet code.


    Approach

    1. Controller Design:

      • Create a UI element (a virtual controller) that includes buttons for each key: arrow keys, space bar, and shift.
      • Make the controller draggable so users can position it anywhere on the screen.
    2. Touchscreen Support:

      • Use touchstart, touchmove, and touchend events to handle multi-touch interactions.
      • Simulate keyboard inputs when the user interacts with the virtual buttons.
    3. Locking Mechanism:

      • Add a toggle button to "lock" the controller in place, preventing it from being dragged.
    4. Keyboard Input Simulation:

      • Use the KeyboardEvent API to simulate key presses (keydown) and releases (keyup) for the underlying website.
    5. Bookmarklet Constraints:

      • Bookmarklets are limited to JavaScript code and must be encapsulated in a single line.
      • The code must be self-contained and not rely on external libraries.

    Bookmarklet Code

    Here is the complete bookmarklet code:

    javascript:(function(){var s=document.createElement('style');s.innerHTML='.controller{position:absolute;bottom:20px;left:20px;background:#333;color:#fff;padding:10px;border-radius:10px;z-index:9999;touch-action:none}.button{background:#555;color:#fff;padding:10px 20px;margin:5px;border-radius:5px;cursor:pointer}.button.active{background:#f00}.lock{position:absolute;top:5px;right:5px;background:#ff0;color:#000;padding:5px;border-radius:5px;cursor:pointer}';document.head.appendChild(s);var c=document.createElement('div');c.className='controller';c.innerHTML='<div class="lock">🔒</div><div class="button" data-key="ArrowUp">↑</div><div class="button" data-key="ArrowLeft">←</div><div class="button" data-key="ArrowDown">↓</div><div class="button" data-key="ArrowRight">→</div><div class="button" data-key="Space">Space</div><div class="button" data-key="Shift">Shift</div>';document.body.appendChild(c);var isLocked=false;c.querySelector('.lock').addEventListener('click',function(){isLocked=!isLocked;this.textContent=isLocked?'🔓':'🔒';});var pos={x:0,y:0};c.addEventListener('touchstart',function(e){if(isLocked)return;var t=e.touches[0];pos.x=t.clientX-c.offsetLeft;pos.y=t.clientY-c.offsetTop;e.preventDefault();});c.addEventListener('touchmove',function(e){if(isLocked)return;var t=e.touches[0];c.style.left=(t.clientX-pos.x)+'px';c.style.top=(t.clientY-pos.y)+'px';e.preventDefault();});c.querySelectorAll('.button').forEach(function(b){b.addEventListener('touchstart',function(e){simulateKey(b.dataset.key,true);b.classList.add('active');e.preventDefault();});b.addEventListener('touchend',function(e){simulateKey(b.dataset.key,false);b.classList.remove('active');e.preventDefault();});});function simulateKey(key,isDown){var event=new KeyboardEvent(isDown?'keydown':'keyup',{key:key,bubbles:true,cancelable:true});document.activeElement.dispatchEvent(event);}})();
    

    Explanation of the Code

    1. Styling:

      • The .controller class styles the draggable container.
      • The .button class styles individual buttons for each key.
      • The .lock class provides a toggle button to lock/unlock the controller.
    2. Controller Creation:

      • A div element is created dynamically and appended to the document body.
      • Buttons for arrow keys, space bar, and shift are added as child elements.
    3. Draggable Functionality:

      • The touchstart event captures the initial touch position relative to the controller.
      • The touchmove event updates the controller's position based on touch movement unless it is locked.
    4. Locking Mechanism:

      • Clicking the lock button toggles the isLocked state, preventing further dragging.
    5. Simulating Key Presses:

      • The simulateKey function creates and dispatches keydown or keyup events for the specified key.
      • These events are sent to the currently focused element, simulating real keyboard input.
    6. Touch Events:

      • Each button listens for touchstart and touchend events to simulate pressing and releasing the corresponding key.

    How to Use the Bookmarklet

    1. Copy the entire bookmarklet code above.
    2. Create a new bookmark in your browser.
    3. Paste the code into the URL field of the bookmark.
    4. Save the bookmark.
    5. Navigate to any website and click the bookmarklet to activate the controller.

    Limitations

    • Some websites may block simulated keyboard events for security reasons.
    • Multi-touch gestures (e.g., pinch-to-zoom) may interfere with the controller.
    • The controller is styled minimally and may need adjustments for specific use cases.

    This bookmarklet provides a robust solution for adding a virtual controller to any website, supporting both touch and keyboard simulation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.