🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1775

Re: Keystroke Simulation

#1775
From: Reid Pinchback
Date: Mon, 01 Oct 2001 13:53
75 lines
2736 bytes

Teng Meng Wah wrote:

> Hi there,
>
> Does anyone have a sample to simulate the keystroke based on the string?

The EventQueue is your friend.  Sample below.


==============================================

package foo;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SimulateKeystrokes
    extends Frame {

  private Simulator sim;

  public SimulateKeystrokes() {
    setBounds(50,50,400,300);
    setLayout(new FlowLayout());
    TextArea area=new TextArea(5,20);
    add(area);
    sim=new Simulator(area);
    Button go=new Button("Say 'Hi There'");
    go.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        sim.simulate("Hi There");
      }
    });
    add(go);
    setVisible(true);
  }

  public static void main(String[] args) {
    new SimulateKeystrokes();
  }

  // ---------------------------------------

  private static class Simulator {

    Component component;

    public Simulator(Component component) {
      this.component=component;
    }

    public void simulate(String text) {
      final int NO_MODIFIERS=0;
      for (int i=0; i < text.length(); i++) {
        char c=text.charAt(i);
        new EventQueue().postEvent(
          new KeyEvent(
            component
            ,KeyEvent.KEY_TYPED
            ,new Date().getTime()
            ,NO_MODIFIERS
            ,KeyEvent.VK_UNDEFINED
            ,c
          )
        );
      }

    }

  }

}
Technical Details
Message-ID: <3BB87590.DDEDD467@mpi.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!paloalto-snh1.gtei.net!news.gtei.net!paloalto-snr1.gtei.net.POSTED!not-for-mail
References: <3bb5f283_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,9f1d35906552f06f
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-10-01 06:53:44 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!paloalto-snh1.gtei.net!news.gtei.net!paloalto-snr1.gtei.net.POSTED!not-for-mail
Message-ID: <3BB87590.DDEDD467@mpi.com>
From: Reid Pinchback <pinchbac@mpi.com>
Organization: Millennium Pharmaceuticals, Inc
X-Mailer: Mozilla 4.76 [en] (WinNT; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: Keystroke Simulation
References: <3bb5f283_2@dnews>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 77
X-Trace: +SLI2lM1GqsKFJjr+5ajv8V9NQLW9g/SrzM7G1cImMS7TSvIAmUXKVSrrdP/M5JAtp/MTm435qQV!xHOtSFFo1opzfO8Hi8ei/mUO94958W6ZHB+rWvkaH+1qF+J5by5Hk3/wVBlloA==
X-Complaints-To: abuse@gte.net
X-Abuse-Info: Please be sure to forward a copy of ALL headers
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
NNTP-Posting-Date: Mon, 01 Oct 2001 13:53:43 GMT
Distribution: world
Date: Mon, 01 Oct 2001 13:53:43 GMT