Keyboard Input Demonstration

When this applet is running, click at a point to position the cursor then type. You will be able to type small green characters where you choose. In the code, notice the empty methods included to comply with the requirements of the interfaces. You can try this and see the next three applets displaying here.

The code of KeyBoardApplet.pas

namespace keyboard_input_applet;
{
  Author: Michael McGuffin
  Converted to Oxygene for Java by Dharmesh Tailor
}
interface

uses
  java.util,
  java.applet.*,
  java.awt,
  java.awt.event;

type
  KeyboardInputApplet = public class(Applet, KeyListener, MouseListener)
  private
    var width, height, x, y : Integer;
    var s : String := '';
  public
    method init; override;
    method keyPressed(e : KeyEvent);
    method keyReleased(e : KeyEvent);
    method keyTyped(e : KeyEvent);
    method mouseEntered(e : MouseEvent);
    method mouseExited(e : MouseEvent);
    method mousePressed(e : MouseEvent);
    method mouseReleased(e : MouseEvent);
    method mouseClicked(e : MouseEvent);
    method paint(g : Graphics); override;
  end;

implementation

method KeyboardInputApplet.init;
begin
  width := getSize.width;
  height := getSize.height;
  setBackground(Color.black);

  x := width / 2;
  y := height / 2;

  addKeyListener(self);
  addMouseListener(self);
end;

method KeyboardInputApplet.keyPressed(e : KeyEvent);
begin
end;

method KeyboardInputApplet.keyReleased(e : KeyEvent);
begin
end;

method KeyboardInputApplet.keyTyped(e : KeyEvent);
var
  c : Char;
begin
  c := e.getKeyChar;
  if (c <> KeyEvent.CHAR_UNDEFINED) then
    begin
      s := s + c;
      repaint;
      e.consume;
    end
end;

method KeyboardInputApplet.mouseEntered(e : MouseEvent);
begin
end;

method KeyboardInputApplet.mouseExited(e : MouseEvent);
begin
end;

method KeyboardInputApplet.mousePressed(e : MouseEvent);
begin
end;

method KeyboardInputApplet.mouseReleased(e : MouseEvent);
begin
end;

method KeyboardInputApplet.mouseClicked(e : MouseEvent);
begin
  self.requestFocusInWindow;
  x := e.X;
  y := e.Y;
  s := '';
  repaint;
  e.consume;
end;

method KeyboardInputApplet.paint(g : Graphics);
begin
  g.setColor(Color.gray);
  g.drawLine(x, y, x, y - 10);
  g.drawLine(x, y, x + 10, y);
  g.setColor(Color.green);
  g.drawString(s, x, y);
end;

end.

A minimal project file keyboard_applet.oxygene

Execute the compiler by typing at the command prompt (with the current directory set to the location of this project file and the source file):

msbuild keyboard_applet.oxygene
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="KeyboardInputApplet.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>

Web page KeyBoardApplet.html to run the applet

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Keyboard Input Demo</title>
  </head>
  <body>
    <center>
      <h3>Keyboard Input Demo</h3>   
      <p>Java must be enabled.</p>  
      <applet archive="keyboard_input.jar"                                   
                    code="keyboard_input_applet/KeyboardInputApplet.class" 
                    width="200" height="200" />    
    </center>
  </body>
</html>

Programming - a skill for life!

Mouse and keyboard input, graphics (including Graphics2D), threads and widgets in Oxygene for Java applets