Groups
Groups
Sign in
Groups
Groups
日本Androidの会
Conversations
About
Send feedback
Help
テストでCtrlキーとの組み合わせたキーをSendKeysする方法
960 views
Skip to first unread message
Rui
unread,
Apr 25, 2011, 2:54:23 AM
4/25/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to 日本Androidの会
こんにちわ、
自分のアプリの単体テストをActivityInstrumentationTestCase2を使い、行おうと思っています。
ある機能を動かすために、Ctrl+BackSpaceのコンビネーションキーをSendKeysで送りたいのですが、どのようにやればいいのかわから
ず困っています。
どなたか助けていただくことは可能でしょうか。
どうぞよろしくお願いいたします。
-Rui
Mickey.S
unread,
Apr 25, 2011, 10:48:12 PM
4/25/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to 日本Androidの会
こんにちは、Mickeyと申します。
InstrumentationTestCase#SendKeys()では、キーの同時押しは送れないようです。
代わりの方法として、android.view.KeyEventクラスのKeyEvent()でキーイベントを生成し、
Instrumentation#sendKeySync()でキーイベントを送信する方法はどうでしょうか。
KeyEvent()はキーイベントを生成するメソッドです。
引数にmetaStateを設定できるKeyEvent()があるので、META_CTRL_ONを設定すれば
CTRLキーを押しながら他のキー同時押しのキーイベントが生成できます。
・android.view.KeyEvent
http://developer.android.com/intl/ja/reference/android/view/KeyEvent.html
・Instrumentation#sendKeySync()
http://developer.android.com/intl/ja/reference/android/app/Instrumentation.html#sendKeySync%28android.view.KeyEvent%29
ちょっと動作確認できていないので、できなかったらすみません。。。
Rui
unread,
Apr 25, 2011, 11:03:30 PM
4/25/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to 日本Androidの会
Mickey様、
大変有益な情報をありがとうございます。さっそくご教授いただいた方法を試してみたいと思います。
結果のほうもここでシェアさせていただきたいと思っていますが、少し時間がかかるかもしれません。
取り急ぎ、お礼を。ありがとうございます。
-Rui
Rui
unread,
Apr 26, 2011, 3:37:24 AM
4/26/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to 日本Androidの会
Mickey様、
InstrumentationのSendKeySync()で期待していた動きを実現できることの確認取れました。
final Instrumentation inst = this.getInstrumentation();
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_A));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_A));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_SHIFT_LEFT));
これでSHIFT+AのSendKeyができていました。
ありがとうございました。無事にやりたかったことが実現させられそうです。
-Rui
Mickey.S
unread,
Apr 26, 2011, 5:06:54 AM
4/26/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to 日本Androidの会
Rui様
Mickeyです。
実現できてよかったです。
ACTION_DOWNとACTION_UPをうまく組み合わせれば
同時押しをエミュレートできるんですね。
私も参考になりました。
ありがとうございます。
Reply all
Reply to author
Forward
0 new messages