當前位置: 首頁>>代碼示例>>Java>>正文


Java KeyCharacterMap.getEvents方法代碼示例

本文整理匯總了Java中android.view.KeyCharacterMap.getEvents方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyCharacterMap.getEvents方法的具體用法?Java KeyCharacterMap.getEvents怎麽用?Java KeyCharacterMap.getEvents使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.view.KeyCharacterMap的用法示例。


在下文中一共展示了KeyCharacterMap.getEvents方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: sendUnicode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Tries to convert a unicode character to a KeyEvent and if successful sends with keyEvent().
 * @param unicodeChar
 * @param metaState
 */
public boolean sendUnicode (char unicodeChar, int additionalMetaState) {
    KeyCharacterMap fullKmap    = KeyCharacterMap.load(KeyCharacterMap.FULL);
    KeyCharacterMap virtualKmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    char[] s = new char[1];
    s[0] = unicodeChar;
    
    KeyEvent[] events = fullKmap.getEvents(s);
    // Failing with the FULL keymap, try the VIRTUAL_KEYBOARD one.
    if (events == null) {
        events = virtualKmap.getEvents(s);
    }
    
    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            KeyEvent evt = events[i];
            processLocalKeyEvent(evt.getKeyCode(), evt, additionalMetaState);
            KeyEvent upEvt = new KeyEvent(KeyEvent.ACTION_UP, evt.getKeyCode());
            processLocalKeyEvent(upEvt.getKeyCode(), upEvt, additionalMetaState);
            return true;
        }
    } else {
        android.util.Log.e("RemoteKeyboard", "Could not use any keymap to generate KeyEvent for unicode: " + unicodeChar);
    }
    return false;
}
 
開發者ID:runsoftdev,項目名稱:bVnc,代碼行數:31,代碼來源:RemoteKeyboard.java

示例2: sendStringSync

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
開發者ID:cuplv,項目名稱:droidel,代碼行數:26,代碼來源:Instrumentation.java

示例3: testInjectKeyEventUpWithNoDown

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
@LargeTest
public void testInjectKeyEventUpWithNoDown() throws Exception {
  sendActivity = getActivity();

  getInstrumentation().runOnMainSync(new Runnable() {
    @Override
    public void run() {
      View view = sendActivity.findViewById(R.id.send_data_edit_text);
      assertTrue(view.requestFocus());
      latch.countDown();
    }
  });

  assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  assertTrue(injector.injectKeyEvent(events[1]));
}
 
開發者ID:DocuSignDev,項目名稱:android-test-kit,代碼行數:19,代碼來源:EventInjectorTest.java

示例4: sendUnicode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Tries to convert a unicode character to a KeyEvent and if successful sends with keyEvent().
 * @param unicodeChar
 */
private void sendUnicode(char unicodeChar) {
    KeyCharacterMap fullKmap    = KeyCharacterMap.load(KeyCharacterMap.FULL);
    KeyCharacterMap virtualKmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    char[] s = new char[1];
    s[0] = unicodeChar;
    
    KeyEvent[] events = fullKmap.getEvents(s);
    // Failing with the FULL keymap, try the VIRTUAL_KEYBOARD one.
    if (events == null) {
        events = virtualKmap.getEvents(s);
    }
    
    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            KeyEvent evt = events[i];
            processLocalKeyEvent(evt);
        }
    } else {
        android.util.Log.w(TAG, "Could not use any keymap to generate KeyEvent for unicode: " + unicodeChar);
    }
}
 
開發者ID:cmusatyalab,項目名稱:vmnetx-android,代碼行數:26,代碼來源:RemoteKeyboard.java

示例5: getAmOrPmKeyCode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
開發者ID:ttpho,項目名稱:TimePicker,代碼行數:35,代碼來源:TimePickerDialog.java

示例6: getAmOrPmKeyCode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
private int getAmOrPmKeyCode(int amOrPm) {
    if (this.mAmKeyCode == -1 || this.mPmKeyCode == -1) {
        KeyCharacterMap kcm = KeyCharacterMap.load(-1);
        int i = 0;
        while (i < Math.max(this.mAmText.length(), this.mPmText.length())) {
            if (this.mAmText.toLowerCase(Locale.getDefault()).charAt(i) != this.mPmText
                    .toLowerCase(Locale.getDefault()).charAt(i)) {
                KeyEvent[] events = kcm.getEvents(new char[]{this.mAmText.toLowerCase(Locale
                        .getDefault()).charAt(i), this.mPmText.toLowerCase(Locale.getDefault
                        ()).charAt(i)});
                if (events == null || events.length != 4) {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                } else {
                    this.mAmKeyCode = events[0].getKeyCode();
                    this.mPmKeyCode = events[2].getKeyCode();
                }
            } else {
                i++;
            }
        }
    }
    if (amOrPm == 0) {
        return this.mAmKeyCode;
    }
    if (amOrPm == 1) {
        return this.mPmKeyCode;
    }
    return -1;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:30,代碼來源:TimePickerDialog.java

示例7: getAmOrPmKeyCode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        final CharSequence amText = mAmText.toLowerCase(mCurrentLocale);
        final CharSequence pmText = mPmText.toLowerCase(mCurrentLocale);
        final int N = Math.min(amText.length(), pmText.length());
        for (int i = 0; i < N; i++) {
            final char amChar = amText.charAt(i);
            final char pmChar = pmText.charAt(i);
            if (amChar != pmChar) {
                // There should be 4 events: a down and up for both AM and PM.
                final KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }

    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
開發者ID:andela-kogunde,項目名稱:CheckSmarter,代碼行數:37,代碼來源:SublimeTimePicker.java

示例8: getAmOrPmKeyCode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == HALF_DAY_1) {
        return mAmKeyCode;
    } else if (amOrPm == HALF_DAY_2) {
        return mPmKeyCode;
    }

    return -1;
}
 
開發者ID:philliphsu,項目名稱:BottomSheetPickers,代碼行數:35,代碼來源:GridTimePickerDialog.java

示例9: getAmOrPmKeyCode

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        final CharSequence amText = mAmText.toLowerCase(mCurrentLocale);
        final CharSequence pmText = mPmText.toLowerCase(mCurrentLocale);
        final int N = Math.min(amText.length(), pmText.length());
        for (int i = 0; i < N; i++) {
            final char amChar = amText.charAt(i);
            final char pmChar = pmText.charAt(i);
            if (amChar != pmChar) {
                // There should be 4 events: a down and up for both AM and PM.
                final KeyEvent[] events = kcm.getEvents(new char[] { amChar, pmChar });
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }

    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
開發者ID:TR4Android,項目名稱:AppCompat-Extension-Library,代碼行數:37,代碼來源:AppCompatTimePickerDelegate.java

示例10: testInjectStaleKeyEvent

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
@LargeTest
public void testInjectStaleKeyEvent() throws Exception {
  sendActivity = getActivity();

  getInstrumentation().runOnMainSync(new Runnable() {
    @Override
    public void run() {
      View view = sendActivity.findViewById(R.id.send_data_edit_text);
      assertTrue(view.requestFocus());
      latch.countDown();
    }
  });

  assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
  assertFalse("SecurityException exception was thrown.", injectEventThrewSecurityException.get());

  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  KeyEvent event = KeyEvent.changeTimeRepeat(events[0], 1, 0);

  // Stale event does not fail for API < 13.
  if (Build.VERSION.SDK_INT < 13) {
    assertTrue(injector.injectKeyEvent(event));
  } else {
    assertFalse(injector.injectKeyEvent(event));
  }
}
 
開發者ID:DocuSignDev,項目名稱:android-test-kit,代碼行數:28,代碼來源:EventInjectorTest.java

示例11: testInjectKeyEvent_securityException

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
@LargeTest
public void testInjectKeyEvent_securityException() {
  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  try {
    injector.injectKeyEvent(events[0]);
    fail("Should have thrown a security exception!");
  } catch (InjectEventSecurityException expected) { }
}
 
開發者ID:DocuSignDev,項目名稱:android-test-kit,代碼行數:10,代碼來源:EventInjectorTest.java

示例12: injectString

import android.view.KeyCharacterMap; //導入方法依賴的package包/類
@Override
public boolean injectString(String str) throws InjectEventSecurityException {
    checkNotNull(str);
    checkState(Looper.myLooper() == mainLooper, "Expecting to be on main thread!");
    initialize();

    // No-op if string is empty.
    if (str.length() == 0) {
        Log.w(TAG, "Supplied string is empty resulting in no-op (nothing is typed).");
        return true;
    }

    boolean eventInjected = false;
    KeyCharacterMap keyCharacterMap = getKeyCharacterMap();

    // TODO(user): Investigate why not use (as suggested in javadoc of keyCharacterMap.getEvents):
    // http://developer.android.com/reference/android/view/KeyEvent.html#KeyEvent(long,
    // java.lang.String, int, int)
    KeyEvent[] events = keyCharacterMap.getEvents(str.toCharArray());
    checkNotNull(events, "Failed to get events for string " + str);
    Log.d(TAG, String.format("Injecting string: \"%s\"", str));

    for (KeyEvent event : events) {
        checkNotNull(event, String.format("Failed to get event for character (%c) with key code (%s)",
                event.getKeyCode(), event.getUnicodeChar()));

        eventInjected = false;
        for (int attempts = 0; !eventInjected && attempts < 4; attempts++) {
            attempts++;

            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            event = KeyEvent.changeTimeRepeat(event, SystemClock.uptimeMillis(), 0);
            eventInjected = injectKeyEvent(event);
        }

        if (!eventInjected) {
            Log.e(TAG, String.format("Failed to inject event for character (%c) with key code (%s)",
                    event.getUnicodeChar(), event.getKeyCode()));
            break;
        }
    }

    return eventInjected;
}
 
開發者ID:DocuSignDev,項目名稱:android-test-kit,代碼行數:49,代碼來源:UiControllerImpl.java


注:本文中的android.view.KeyCharacterMap.getEvents方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。