view snippets/display/Snippet146.d @ 132:104f5ed240fc

More snippets from TomD, thanks!
author Frank Benoit <benoit@tionex.de>
date Tue, 29 Jul 2008 01:50:10 +0200
parents
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * D Port:
 *     Thomas Demmer <t_demmer AT web DOT de>
 *******************************************************************************/
module display.Snippet146;
  
/*
 * UI Automation (for testing tools) snippet: post key events
 *
 * For a list of all DWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import dwt.DWT;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Shell;
import dwt.widgets.Text;

import dwt.dwthelper.utils;

import tango.core.Thread;
import tango.text.Unicode;

void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Text text = new Text(shell, DWT.BORDER);
    text.setSize(text.computeSize(150, DWT.DEFAULT));
    shell.pack();
    shell.open();
    Thread thread = new Thread({
        String string = "Love the method.";
        String lstring;
        lstring. length = string.length;
        toLower(string, lstring);
        for (int i = 0; i < string.length; i++) {
            char ch = string.charAt(i);
            bool shift = isUpper(ch);
            ch = lstring.charAt(i);
            if (shift) {
                Event event = new Event();
                event.type = DWT.KeyDown;
                event.keyCode = DWT.SHIFT;
                display.post(event);    
            }
            Event event = new Event();
            event.type = DWT.KeyDown;
            event.character = ch;
            display.post(event);
            try {
                Thread.sleep(10/1000.);
            } catch (InterruptedException e) {}
            event.type = DWT.KeyUp;
            display.post(event);
            try {
                Thread.sleep(100/1000.0);
            } catch (InterruptedException e) {}
            if (shift) {
                event = new Event();
                event.type = DWT.KeyUp;
                event.keyCode = DWT.SHIFT;
                display.post(event);    
            }
        }
    });
    thread.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
}