/tags/20100815/src/com/menny/android/anysoftkeyboard/HardKeyboardActionImpl.java

http://softkeyboard.googlecode.com/ · Java · 170 lines · 35 code · 12 blank · 123 comment · 2 complexity · b3bf8c441a8518e096725545fdb8de0d MD5 · raw file

  1. package com.menny.android.anysoftkeyboard;
  2. import android.text.method.MetaKeyKeyListener;
  3. import android.view.KeyEvent;
  4. import com.menny.android.anysoftkeyboard.keyboards.AnyKeyboard.HardKeyboardAction;
  5. //public class HardKeyboardActionImpl implements HardKeyboardAction
  6. //{
  7. // private enum MetaKeyState
  8. // {
  9. // Off,
  10. // On,
  11. // Pressed,
  12. // Sticky
  13. // }
  14. //
  15. // public MetaKeyState mPhysicalShiftState = MetaKeyState.Off;
  16. // public MetaKeyState mPhysicalAltState = MetaKeyState.Off;
  17. // private int mKeyCode = 0;
  18. // private boolean mChanegd = false;
  19. //
  20. // public void resetMetaState()
  21. // {
  22. // mPhysicalShiftState = MetaKeyState.Off;
  23. // mPhysicalAltState = MetaKeyState.Off;
  24. // mKeyCode = 0;
  25. // mChanegd = false;
  26. // }
  27. //
  28. // public boolean initializeAction(KeyEvent event)
  29. // {
  30. // mChanegd = false;
  31. // mKeyCode = event.getKeyCode();
  32. // switch (mKeyCode)
  33. // {
  34. // case KeyEvent.KEYCODE_SHIFT_LEFT:
  35. // case KeyEvent.KEYCODE_SHIFT_RIGHT:
  36. // if (event.getRepeatCount() == 0)
  37. // {
  38. // mPhysicalShiftState = getNextStateOnMetaKeyPress(mPhysicalShiftState, event.isShiftPressed());
  39. // if (AnySoftKeyboard.getDEBUG())
  40. // Log.d("AnySoftKeyboard", "Physical SHIFT was pressed. The new shift state is "+mPhysicalShiftState);
  41. // }
  42. // return true;
  43. // case KeyEvent.KEYCODE_ALT_LEFT:
  44. // case KeyEvent.KEYCODE_ALT_RIGHT:
  45. // if (event.getRepeatCount() == 0)
  46. // {
  47. // mPhysicalAltState = getNextStateOnMetaKeyPress(mPhysicalAltState, event.isAltPressed());
  48. // if (AnySoftKeyboard.getDEBUG())
  49. // Log.d("AnySoftKeyboard", "Physical ALT was pressed. The new ALT state is "+mPhysicalAltState);
  50. // }
  51. // return true;
  52. // default:
  53. // //if it sticky, then it will stay.
  54. // //else
  55. // //if meta-key is pressed, then on else stay as is (may be consumed by the key translation)
  56. // mPhysicalShiftState = getNextStateOnRegularKey(mPhysicalShiftState, event.isShiftPressed());
  57. // mPhysicalAltState = getNextStateOnRegularKey(mPhysicalAltState, event.isAltPressed());
  58. // return false;
  59. // }
  60. // }
  61. //
  62. // private static MetaKeyState getNextStateOnRegularKey(MetaKeyState currentState, boolean isPressed) {
  63. // switch(currentState)
  64. // {
  65. // case Off:
  66. // case Pressed:
  67. // return isPressed? MetaKeyState.Pressed : MetaKeyState.Off;
  68. // case On:
  69. // return isPressed? MetaKeyState.Pressed : MetaKeyState.On;
  70. // case Sticky:
  71. // return MetaKeyState.Sticky;
  72. // default:
  73. // return MetaKeyState.Off;
  74. // }
  75. // }
  76. //
  77. // private static MetaKeyState getNextStateOnMetaKeyPress(MetaKeyState currentState, boolean isPressed)
  78. // {
  79. // if (isPressed)
  80. // {
  81. // switch(currentState)
  82. // {
  83. // case Off:
  84. // case Pressed:
  85. // return MetaKeyState.On;
  86. // case On:
  87. // return MetaKeyState.Sticky;
  88. // case Sticky:
  89. // return MetaKeyState.Off;
  90. // }
  91. // }
  92. //
  93. // return MetaKeyState.Off;
  94. // }
  95. //
  96. // public int getKeyCode() {
  97. // return mKeyCode;
  98. // }
  99. //
  100. // public int consumeKeyCode() {
  101. // //consuming
  102. // if (mPhysicalAltState == MetaKeyState.On)
  103. // mPhysicalAltState = MetaKeyState.Off;
  104. // if (mPhysicalShiftState == MetaKeyState.On)
  105. // mPhysicalShiftState = MetaKeyState.Off;
  106. //
  107. // return mKeyCode;
  108. // }
  109. //
  110. // public boolean isAltActive() {
  111. // return mPhysicalAltState != MetaKeyState.Off;
  112. // }
  113. //
  114. // public boolean isShiftActive() {
  115. // return mPhysicalShiftState != MetaKeyState.Off;
  116. // }
  117. //
  118. // public void setNewKeyCode(int keyCode) {
  119. // mChanegd = true;
  120. // mKeyCode = keyCode;
  121. // }
  122. //
  123. // public boolean getKeyCodeWasChanged()
  124. // {
  125. // return mChanegd;
  126. // }
  127. //}
  128. public class HardKeyboardActionImpl implements HardKeyboardAction
  129. {
  130. private int mKeyCode = 0;
  131. private boolean mChanegd = false;
  132. private long mMetaState;
  133. private final int META_ACTIVE_ALT = (MetaKeyKeyListener.META_ALT_ON | MetaKeyKeyListener.META_ALT_LOCKED);
  134. private final int META_ACTIVE_SHIFT = (MetaKeyKeyListener.META_SHIFT_ON | MetaKeyKeyListener.META_CAP_LOCKED);
  135. public void initializeAction(KeyEvent event, long metaState)
  136. {
  137. mChanegd = false;
  138. mKeyCode = event.getKeyCode();
  139. mMetaState = metaState;
  140. }
  141. public int getKeyCode() {
  142. return mKeyCode;
  143. }
  144. public boolean isAltActive() {
  145. return (MetaKeyKeyListener.getMetaState(mMetaState) & META_ACTIVE_ALT) != 0;
  146. }
  147. public boolean isShiftActive() {
  148. return (MetaKeyKeyListener.getMetaState(mMetaState) & META_ACTIVE_SHIFT) != 0;
  149. }
  150. public void setNewKeyCode(int keyCode) {
  151. mChanegd = true;
  152. mKeyCode = keyCode;
  153. }
  154. public boolean getKeyCodeWasChanged()
  155. {
  156. return mChanegd;
  157. }
  158. }