1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This class describe a keyboard accelerator (or keyboard shortcut).
6 // Keyboard accelerators are registered with the FocusManager.
7 // It has a copy constructor and assignment operator so that it can be copied.
8 // It also defines the < operator so that it can be used as a key in a std::map.
11 #ifndef UI_BASE_ACCELERATORS_ACCELERATOR_H_
12 #define UI_BASE_ACCELERATORS_ACCELERATOR_H_
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "ui/base/accelerators/platform_accelerator.h"
17 #include "ui/base/ui_base_export.h"
18 #include "ui/events/event_constants.h"
19 #include "ui/events/keycodes/keyboard_codes.h"
24 class PlatformAccelerator
;
26 // This is a cross-platform class for accelerator keys used in menus.
27 // |platform_accelerator| should be used to store platform specific data.
28 class UI_BASE_EXPORT Accelerator
{
31 Accelerator(ui::KeyboardCode keycode
, int modifiers
);
32 explicit Accelerator(const KeyEvent
& key_event
);
33 Accelerator(const Accelerator
& accelerator
);
36 Accelerator
& operator=(const Accelerator
& accelerator
);
38 // Define the < operator so that the KeyboardShortcut can be used as a key in
40 bool operator <(const Accelerator
& rhs
) const;
42 bool operator ==(const Accelerator
& rhs
) const;
44 bool operator !=(const Accelerator
& rhs
) const;
46 ui::KeyboardCode
key_code() const { return key_code_
; }
48 // Sets the event type if the accelerator should be processed on an event
49 // other than ui::ET_KEY_PRESSED.
50 void set_type(ui::EventType type
) { type_
= type
; }
51 ui::EventType
type() const { return type_
; }
53 int modifiers() const { return modifiers_
; }
55 bool IsShiftDown() const;
56 bool IsCtrlDown() const;
57 bool IsAltDown() const;
58 bool IsCmdDown() const;
59 bool IsRepeat() const;
61 // Returns a string with the localized shortcut if any.
62 base::string16
GetShortcutText() const;
64 void set_platform_accelerator(scoped_ptr
<PlatformAccelerator
> p
) {
65 platform_accelerator_
= p
.Pass();
68 // This class keeps ownership of the returned object.
69 const PlatformAccelerator
* platform_accelerator() const {
70 return platform_accelerator_
.get();
73 void set_is_repeat(bool is_repeat
) { is_repeat_
= is_repeat
; }
76 // The keycode (VK_...).
77 KeyboardCode key_code_
;
79 // The event type (usually ui::ET_KEY_PRESSED).
82 // The state of the Shift/Ctrl/Alt keys.
85 // True if the accelerator is created for an auto repeated key event.
88 // Stores platform specific data. May be NULL.
89 scoped_ptr
<PlatformAccelerator
> platform_accelerator_
;
92 // An interface that classes that want to register for keyboard accelerators
94 class UI_BASE_EXPORT AcceleratorTarget
{
96 // Should return true if the accelerator was processed.
97 virtual bool AcceleratorPressed(const Accelerator
& accelerator
) = 0;
99 // Should return true if the target can handle the accelerator events. The
100 // AcceleratorPressed method is invoked only for targets for which
101 // CanHandleAccelerators returns true.
102 virtual bool CanHandleAccelerators() const = 0;
105 virtual ~AcceleratorTarget() {}
108 // Since accelerator code is one of the few things that can't be cross platform
109 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from
110 // the menu delegates.
111 class AcceleratorProvider
{
113 // Gets the accelerator for the specified command id. Returns true if the
114 // command id has a valid accelerator, false otherwise.
115 virtual bool GetAcceleratorForCommandId(int command_id
,
116 ui::Accelerator
* accelerator
) = 0;
119 virtual ~AcceleratorProvider() {}
124 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_