---
category: swing
folder: FocusTraversalKeys
title: FocusTraversalKeysに矢印キーを追加してフォーカス移動
tags: [KeyboardFocusManager, Focus, Container]
author: aterai
pubdate: 2008-06-02T13:10:23+09:00
description: デフォルトのKBD{Tab}キーに加えて、矢印キーでもフォーカス移動できるように設定します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTNHR5gShI/AAAAAAAAAaQ/KUE3fbR0bXo/s800/FocusTraversalKeys.png
---
* 概要 [#summary]
デフォルトのKBD{Tab}キーに加えて、矢印キーでもフォーカス移動できるように設定します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTNHR5gShI/AAAAAAAAAaQ/KUE3fbR0bXo/s800/FocusTraversalKeys.png)

* サンプルコード [#sourcecode]
#code(link){{
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();

Set<AWTKeyStroke> forwardKeys = new HashSet<>(
    focusManager.getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
focusManager.setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardKeys);

Set<AWTKeyStroke> backwardKeys = new HashSet<>(
    focusManager.getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
backwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0));
backwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0));
focusManager.setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardKeys);
}}

* 解説 [#explanation]
上記のサンプルでは、デフォルトのフォーカストラバーサルキー(KBD{Tab}、KBD{Shift+Tab})に加えて、上下左右の矢印キーでもフォーカス移動できるように設定しています。

- `KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS`
-- `KeyEvent.VK_RIGHT`, `KeyEvent.VK_DOWN`
- `KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS`
-- `KeyEvent.VK_LEFT`, `KeyEvent.VK_UP`

----
各`JFrame`や`JDialog`に別々の`FocusTraversalKeys`を設定したい場合は、`Container#setFocusTraversalKeys`を使用します。
- 各`JFrame`や`JDialog`に別々の`FocusTraversalKeys`を設定したい場合は、以下のように`Container#setFocusTraversalKeys`を使用する

#code{{
Set<AWTKeyStroke> forwardKeys = new HashSet<>(
    frame.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
frame.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardKeys);
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/KeyboardFocusManager.html KeyboardFocusManager (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Container.html#setFocusTraversalKeys-int-java.util.Set- Container#setFocusTraversalKeys(...) (Java Platform SE 8)]
- [https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html How to Use the Focus Subsystem]
- [[Focusの移動>Swing/FocusTraversal]]

* コメント [#comment]
#comment
#comment