Skip to content

Commit eec40ff

Browse files
authored
Merge pull request #364 from rdkcentral/over-ride-keymap
Over ride keymap
2 parents 24c1ca8 + 080c0c9 commit eec40ff

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Plugins
44
- [Accessibility](/plugins/accessibility.md)
5+
- [App](/plugins/app.md)
56
- [Utils](/plugins/utils.md)
67
- [Storage](/plugins/storage.md)
78
- [Settings](/plugins/settings.md)

docs/plugins/app.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# App
2+
3+
SDK provides various methods that can be used within the application.
4+
5+
## Available Methods
6+
7+
### overRideKeyMap
8+
9+
This method can be used to merge and override the existing keymap (coming from `settings.json`) by using a custom keymap in runtime. The method will merge all key mappings by overriding the existing key mappings with the new ones when a key is defined in both keymaps.
10+
11+
`keepDuplicates` flag is set to `false` by default, which means duplicated values (like two different keys trigger the `Back` action) will be removed from the final keymap. If `keepDuplicates` is set to `true`, duplicated **values** will be kept in the final keymap.
12+
13+
#### Usage
14+
15+
```js
16+
const customKeyMap = {
17+
13: "Enter",
18+
83: "Search"
19+
}
20+
21+
this.application.overRideKeyMap(customKeyMap) // keepDuplicates = false
22+
```
23+
24+
or
25+
26+
```js
27+
this.application.overRideKeyMap(customKeyMap, true) // keepDuplicates = true
28+
```
29+
30+
#### Parameters
31+
32+
- `customKeyMap` : keymap object
33+
- `keepDuplicates`: flag to keep duplicated values in the final keymap or not. The default value is `false`.

src/Application/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ export default function(App, appData, platformSettings) {
119119
Accessibility.colorshift(this, type, config)
120120
}
121121

122+
get keymapping() {
123+
return this.stage.application.config.keys
124+
}
125+
126+
/**
127+
* This function overides the default keymap with the latest keymap.
128+
* @param customKeymap
129+
* @param keepDuplicates
130+
*/
131+
overRideKeyMap(customKeymap, keepDuplicates = false) {
132+
const baseKeymap = this.stage.application.config.keys
133+
Object.keys(customKeymap).reduce((keymapping, key) => {
134+
// prevent duplicate values to exist in final keymapping (i.e. 2 keys triggering 'Back')
135+
if (!keepDuplicates) {
136+
Object.keys(baseKeymap).forEach(baseKey => {
137+
if (baseKey != key && baseKeymap[baseKey] == customKeymap[key]) {
138+
delete keymapping[baseKey]
139+
}
140+
})
141+
}
142+
keymapping[key] = customKeymap[key]
143+
return keymapping
144+
}, baseKeymap)
145+
return baseKeymap
146+
}
147+
122148
_setup() {
123149
Promise.all([
124150
this.loadFonts((App.config && App.config.fonts) || (App.getFonts && App.getFonts()) || []),

0 commit comments

Comments
 (0)