<- previous    index    next ->

Lecture 6, Getting user input

In GUI applications, the code to get user input is much more
complex than the code for a command line program.

Much user input is via the mouse, button press or motion.
Some user input is from the keyboard.

You have the power to really mess up the user.
Make the user click the mouse then type on the keyboard then
click the mouse then type on the keyboard then click the
mouse then type on the keyboard, etc. etc. etc.

A user friendly interface has menus, buttons, graphics, etc
to allow the user many steps with the mouse before touching
the keyboard. Then when the keyboard is needed, allow the user
to perform many steps before having to go back to the mouse.

Mouse button press input in examples:
w2.c  X Windows

w2gl.c  OpenGL

W2frame.java  Java
(run and click 4 corners, see coordinates)

w2tkm.py  Python Tk just mouse
w2tk.py  Python Tk

canvas_mouse.txt view  html5
canvas_mouse.html  html5
(move mouse, see coordinates)

Getting input data, text, into a graphical user interface program
is much more difficult. The input of numbers is accomplished by
inputting a character string and converting the character string
to a number inside the program.

The X Windows Motif program w5a has one pull-down menu on the menu bar,
"Set Values". On the pull-down there are five menu items:
  "Number"
  "View Angle"
  "Name"
  "Apply"
  "Quit"

The selecting one of the first three causes a popup dialog box to
appear. The dialog box is where the user enters the data.








The source code is:
w5a.c  X Windows
w5.h 

In OpenGL and GLUT I wrote my own data entry in fixed windows.
These could be made into separate windows and could be
selected by a menu.
test_text_in.c  OpenGL
demo_text_in.c
text_in.h
text_in.c

Now, add a popup dialog window for user data entry.
w5gl.c


The Java implementation is the shortest.
Similar "toolkit" classes should be available for
Microsoft C++ and C#.

W5frame.java  Java








Another simple screen input/output Java program is Fahrenheit.java


For games or interactive keyboard use, you will need a key listener.
The events are key pressed, key typed, and key released.
You probably only want key released and a switch on "keyCode"
TestKeyCode.java
with output for key sequence  a A 0 space right left up down ctrl enter 
TestKeyCode.out

In Python, to get keysyms, catch an event:
test_keysym.py
test_keysym_py.out


When you can input text, a character string, then you can
use your programming language to open and read and write files.
You can get numeric data when needed but do not use this as
a substitute for mouse input for numeric data,
with possible use a grid for accuracy.

For picture or numeric data, to see plot, scale:
grid.py3  source code
grid.py3  source code




A sample Java Swing  File-Open  then select a file is:
W4frame.java  that uses
ExampleFileFilter.java

A crude sample of reading a directory in C is dirprt.c
This could be used in an OpenGL application with a display of the
file names, selectable by using the mouse. Basically, a do-it-yourself
file open dialog box.

The File-open is handled by a widget in Motif as shown in
the callback function DoOpen XmOpen.c

    <- previous    index    next ->

Other links

Many web sites on Java GUI, AWT, Swing, etc.
Many web sites on Python wx, tk, qt, etc.

Go to top