diff --git a/src/orca/orca.py b/src/orca/orca.py
index ac300e4..bec9306 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -1,6 +1,6 @@
 # Orca
 #
-# Copyright 2004-2008 Sun Microsystems Inc.
+# Copyright 2004-2009 Sun Microsystems Inc.
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Library General Public
@@ -22,7 +22,7 @@
 __id__        = "$Id$"
 __version__   = "$Revision$"
 __date__      = "$Date$"
-__copyright__ = "Copyright (c) 2004-2008 Sun Microsystems Inc."
+__copyright__ = "Copyright (c) 2004-2009 Sun Microsystems Inc."
 __license__   = "LGPL"
 
 # We're going to force the name of the app to "orca" so pygtk
@@ -57,6 +57,7 @@ except RuntimeError:
 
 import getopt
 import os
+import re
 import signal
 import time
 import unicodedata
@@ -99,6 +100,37 @@ _userSettings = None
 #
 _commandLineSettings = {}
 
+# The state of the xmodmap modifiers before orca modifier keys are
+# allowed to effect the system.  Note this variable should be
+# read-only.  The keys are the keysyms for the modifiers and the
+# values are the actual modifiers controlled by the keysyms.
+#
+def _getXmodmapModifiers():
+    mods = {}
+    cmd = "xmodmap | tail -n 9 | head -n 8 " \
+          "| sed -e 's/([^)]*)//g' -e 's/,//g'"
+    filehandle = os.popen(cmd)
+    txt = filehandle.read()
+    filehandle.close()
+    lines = txt.splitlines()
+    for i in range(0, len(lines)):
+        lines[i] = re.sub('\ +', ' ', lines[i])
+        lines[i] = re.sub('\ +$', '', lines[i])
+        lst = lines[i].split(" ")
+        tag = lst[0]
+        for i in lst[1:]:
+            mods[i] = tag
+    return mods
+
+_xmodmapModifiers = _getXmodmapModifiers()
+
+# A hash of keys, from _xmodmapModifiers that had to be cleared when
+# the orca modifier keys were set. The keys are the keysyms for the
+# modifiers and the values are the actual modifiers controlled by the
+# keysyms.
+#
+_clearedXmodmapModifiers = {}
+
 ########################################################################
 #                                                                      #
 # METHODS FOR HANDLING PRESENTATION MANAGERS                           #
@@ -1049,31 +1081,41 @@ def loadUserSettings(script=None, inputEvent=None):
     except NameError:
         pass
 
-    # We don't want the Caps_Lock modifier to act as a locking
-    # modifier if it used as the Orca modifier key.  In addition, if
-    # the KP_Insert key is used as the Orca modifier key, we want to
-    # make sure we clear any other keysyms that might be in use on
-    # that key since we won't be able to detect them as being the Orca
-    # modifier key.  For example, KP_Insert produces "KP_Insert" when
-    # pressed by itself, but Shift+KP_Insert produces "0".
-    #
-    # The original values are saved/reset in the orca shell script.
-    #
-    # [[[TODO: WDW - we probably should just to a 'xmodmap -e "%s = %s"'
-    # for all of the orcaModifierKeys, but saving/restoring the values
-    # becomes a little more difficult.  If we could assume a writeable
-    # filesystem (we cannot), we could do a 'xmodmap -pke > /tmp/foo'
-    # to save the keymap and a 'xmodmap /tmp/foo' to restore it.
-    # For now, we'll just look at the Orca modifier keys we support
-    # (Caps Lock, KP_Insert, and Insert).]]]
-    #
+    clearedModifiers = {}
     for keyName in settings.orcaModifierKeys:
-        if keyName == "Caps_Lock":
-            os.system('xmodmap -e "clear Lock"')
-        if keyName in ["Caps_Lock", "KP_Insert", "Insert"]:
+        # We don't want an X Windows System modifier key to change the
+        # X modifier if it is used as the Orca modifier key.
+        #
+        if keyName in _xmodmapModifiers:
+            removeCmd = 'xmodmap -e "remove ' \
+                        + _xmodmapModifiers[keyName] + ' = ' + keyName + '"'
+            _clearedXmodmapModifiers[keyName] = True
+            clearedModifiers[keyName] = True
+            os.system(removeCmd)
+
+        # In addition, if the KP_Insert key is used as the Orca
+        # modifier key, we want to make sure we clear any other
+        # keysyms that might be in use on that key since we won't be
+        # able to detect them as being the Orca modifier key.  For
+        # example, KP_Insert produces "KP_Insert" when pressed by
+        # itself, but Shift+KP_Insert produces "0". The original
+        # values are saved/reset in the orca shell script.
+        #
+        if keyName in ["KP_Insert", "Insert"]:
             command = 'xmodmap -e "keysym %s = %s"' % (keyName, keyName)
             os.system(command)
 
+    # If some keys were previously the Orca key and were also
+    # originally a normal X Window System modifier, we may need to
+    # re-enable them as modifiers if they are no longer the Orca key.
+    #
+    for i in _clearedXmodmapModifiers:
+        if _clearedXmodmapModifiers[i] and i not in clearedModifiers:
+            addCmd = ' xmodmap -e "add ' \
+                     + _xmodmapModifiers[i] + ' = ' + i + '"'
+            _clearedXmodmapModifiers[i] = False
+            os.system(addCmd)
+
     if _currentPresentationManager >= 0:
         _PRESENTATION_MANAGERS[_currentPresentationManager].activate()
 
@@ -1302,6 +1344,19 @@ def start(registry):
 
 def die(exitCode=1):
 
+    # Restore any xmodmap modifiers that we might have changed.
+    #
+    # We know what we are doing,
+    # disable warning about i being possibly undefined.
+    # pylint: disable-msg=W0631
+
+    for i in _clearedXmodmapModifiers:
+        if _clearedXmodmapModifiers[i]:
+            addCmd = ' xmodmap -e "add ' \
+                     + _xmodmapModifiers[i] + ' = ' + i + '"'
+            _clearedXmodmapModifiers[i] = False
+            os.system(addCmd)
+
     # We know what we are doing here, so tell pylint not to flag
     # the _exit method call as a warning.  The disable-msg is
     # localized to just this method.
@@ -1365,6 +1420,18 @@ def shutdown(script=None, inputEvent=None):
         signal.alarm(0)
 
     _initialized = False
+
+    # We know what we are doing, so disable pylint warning
+    # about i being possibly undefined.
+    # pylint: disable-msg=W0631
+
+    for i in _clearedXmodmapModifiers:
+        if _clearedXmodmapModifiers[i]:
+            addCmd = ' xmodmap -e "add ' \
+                     + _xmodmapModifiers[i] + ' = ' + i + '"'
+            _clearedXmodmapModifiers[i] = False
+            os.system(addCmd)
+
     return True
 
 exitCount = 0
