2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Sun Microsystems Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
28 #define LABELMAXLEN 20
30 #define CAPSLOCK_KEYCODE 66
32 /* these can be increased to access more keys */
34 #define MAX_COLUMNS 14
36 static KeystrokeListener *key_listener;
38 static boolean shift_latched = False;
39 static boolean caps_lock = False;
40 static GtkButton **buttons[MAX_ROWS];
47 KeyCode keycode = MIN_KEYCODE;
48 char label[LABELMAXLEN] = " ";
53 for (i=0; i<MAX_ROWS; ++i)
55 for (j=0; j<MAX_COLUMNS; ++j, ++keycode)
57 shifted = caps_lock || shift_latched;
58 keysym = (KeySym) XKeycodeToKeysym (GDK_DISPLAY(), keycode, shifted ? 1 : 0);
59 /* Note: these routines are not i18n-savvy, we need to use XIM, other methods here */
60 if (keysym && g_ascii_isprint((int)keysym))
62 snprintf (label, 2, "%c", (int) keysym);
66 keysymstring = XKeysymToString (keysym);
69 /* KP_ means keypad... we won't expose this difference */
70 if (!strncmp (keysymstring, "KP_", 3))
71 strncpy (label, (char *)(keysymstring+3), LABELMAXLEN);
72 else strncpy (label, keysymstring, LABELMAXLEN);
77 *label==' ' ? " space " : label;
78 gtk_button_set_label (buttons[i][j], button_label);
84 do_shift (GtkButton *button)
86 static KeyCode shift_keycode = 0;
87 if (!shift_keycode) shift_keycode = XKeysymToKeycode(GDK_DISPLAY(), (KeySym) 0xFFE1);
88 /* Note: in a real onscreen keyboard shift keycode should not be hard-coded! */
89 shift_latched = !shift_latched;
90 generateKeyEvent (shift_keycode, shift_latched ? KEY_PRESS : KEY_RELEASE);
91 if (buttons) label_buttons (buttons);
97 if (shift_latched) do_shift (NULL);
98 deregisterKeystrokeListener (key_listener, KEYMASK_ALT );
103 button_exit(GtkButton *notused, void *alsonotused)
109 is_command_key (void *p)
111 KeyStroke *key = (KeyStroke *)p;
117 return TRUE; /* not reached */
122 synth_keycode (GtkButton *button, KeyCode *keycode)
124 if (*keycode) generateKeyEvent ((long) *keycode, KEY_PRESSRELEASE);
125 if (shift_latched) do_shift (button);
126 if (*keycode == CAPSLOCK_KEYCODE)
128 caps_lock = !caps_lock;
136 GtkWidget *window, *button, *container, *hbox;
138 KeyCode *keycodeptr, keycode = MIN_KEYCODE;
140 window = g_object_connect (gtk_widget_new (gtk_window_get_type (),
143 "type", GTK_WINDOW_POPUP,
144 "window-position", GTK_WIN_POS_CENTER,
147 "allow_shrink", FALSE,
150 "signal::destroy", keysynth_exit, NULL,
153 container = gtk_widget_new (GTK_TYPE_VBOX,
154 "GtkWidget::parent", window,
155 "GtkWidget::visible", TRUE,
157 for (i=0; i<MAX_ROWS; ++i)
159 hbox = gtk_widget_new (gtk_hbox_get_type(),
160 "GtkWidget::parent", container,
161 "GtkWidget::visible", TRUE,
163 buttons[i] = g_new0 (GtkButton*, MAX_COLUMNS);
164 for (j=0; j<MAX_COLUMNS; ++j)
166 keycodeptr = (KeyCode *) g_new0 (KeyCode, 1);
167 *keycodeptr = keycode;
169 buttons[i][j] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
170 "GtkWidget::parent", hbox,
171 "GtkWidget::visible", TRUE,
174 synth_keycode, keycodeptr,
178 hbox = gtk_widget_new (gtk_hbox_get_type(),
179 "GtkWidget::parent", container,
180 "GtkWidget::visible", TRUE,
182 button = g_object_connect (gtk_widget_new (gtk_button_get_type (),
183 "GtkButton::label", "Quit",
184 "GtkWidget::parent", hbox,
185 "GtkWidget::visible", TRUE,
190 button = g_object_connect (gtk_widget_new (gtk_button_get_type (),
191 "GtkButton::label", "ShiftLatch",
192 "GtkWidget::parent", hbox,
193 "GtkWidget::visible", TRUE,
199 gtk_widget_show (window);
204 main(int argc, char **argv)
206 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
208 printf ("Usage: keysynth-demo\n");
212 gtk_init (&argc, &argv); /* must call, because this program uses GTK+ */
216 key_listener = createKeystrokeListener(is_command_key);
217 /* will listen only to Alt-key combinations */
218 registerKeystrokeListener(key_listener,
221 (unsigned long) ( KeyPress | KeyRelease),
222 KEYLISTENER_CANCONSUME);
225 SPI_event_main(TRUE);