This article is currently in the process of being translated into Korean (~18% done).

컨트롤 컨셉:

Access Keys

액셀러레이터 키단축키라고 불리는 액세스 키는 Alt 키를 누른 채로 다른 키를 눌러 윈도우 내의 특정 컨트롤에 접근하는 방법입니다. 마우스를 사용하지 않고 키보드로만 윈도우를 조작할 수 있기 때문에, 액세스 키를 사용하면 윈도우의 편의성이 높아집니다.

Defining Access Keys

WPF 컨트롤의 액세스 키를 정의하기는 쉽지만, 방법이 다소 놀랍습니다. 보통 이런 경우 속성을 사용하겠지만, 액세스 키는 그렇지 않습니다. 그 대신, Text 나 Content 속성 내의 문자 앞에 언더스코어(_)를 붙이는 것으로 액세스 키를 정의합니다. 예를 들면 다음과 같습니다:

<Button Content="_New"></Button>

문자 N 앞에 언더스코어(_)가 붙은 것을 보시기 바랍니다 - 이렇게 하면 N 키가 이 버튼 컨트롤의 액세스 키가 됩니다. 아래 예제에서는 모든 버튼에 액세스 키를 할당했는데, 보시다시피 기본 상태에선 컨트롤의 외관이 변하지는 않습니다:

하지만, Alt 키를 누르는 순간, 사용 가능한 액세스 키가 밑줄로 표시됩니다:

Alt 키를 누른 채로, 액세스 키(e.g. N, O 또는 S)를 눌러 특정 버튼을 작동시킬 수 있습니다. 버튼의 동작은 마우스로 클릭했을 때와 동일합니다.

Access Keys are fine for single elements in a dialog/window, but they are even more useful in the traditional Windows Menus, where you will usually need to click your way through a hierarchy of menu items before reaching the one you need. Here's an example from Visual Studio:

In this case, instead of having to navigate through the menu with several mouse moves and clicks when I want to start a new Project, I can hold down the Alt key and then press F (for File), then N (for New) and then P (for Project). Sure, this could also have been accomplished with the regular keyboard shortcut (Ctrl+Shift+N), but that shortcut is not visible until you reach the last level of the menu hierarchy, so unless you have it memorized already, it might be easier to use the Access Keys, since they are visually highlighted as soon as you press the Alt key.

Which character(s) should be used as Access Keys?

You might be tempted to just use any of the characters found in the control text/content, but there are actually guidelines for picking the right character. The most important rule is of course to pick a character not used by another control already, but in addition to that, you should use the following guidelines:

  • Use the first character of the first word
  • If that's not possible, use the first character of the second or third word (e.g. the A in Save As)
  • If that's not possible, use the second character of the first word (e.g. P in Open)
  • If that's not possible, use the second character of the second or third word (e.g. the l in Save All)
  • In general, you may want to avoid narrow characters like i and l, and go for the wider characters like m, s, w etc.

Tying together two controls

In the examples we have seen so far, we have been able to define the Access Key directly on the control we want to reach. But there's at least one example where this isn't directly possible: When you have an input control, e.g. a TextBox, the text that indicate its purpose doesn't exist within the actual TextBox control. Instead, you would usually use a second control to indicate, with text, the purpose of the TextBox control. This would usually be a Label control.

So, in this example, the Label control would then hold the descriptive text, and therefore also the Access Key, but the control you want to give attention to would be the TextBox control. No problem - we can use the Target property of the Label to tie it together with the TextBox (or any other control), like this:

<StackPanel Margin="20">
    <Label Content="_First name:" Target="{Binding ElementName=txtFirstName}" />
    <TextBox Name="txtFirstName" />
    <Label Content="_Last name:" Target="{Binding ElementName=txtLastName}" />
    <TextBox Name="txtLastName" />
    <Button Content="_Save" Margin="20"></Button>

Notice how the Access Key is specified for the Label controls and then tied to the relevant TextBox control using the Target property, where we use an ElementName based Binding to do the actual work. Now we can access the two TextBox controls using Alt+F and Alt+L, and the Button with Alt+S. Here's how it looks:


By using Access Keys in your windows/dialogs, you are making it much easier for people to navigate using only their keyboards. This is especially popular among power-users, who will use the keyboard in favor of the mouse as much as possible. You should always use Access Keys, especially for your menus.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!