Input Device: Example operations with InputDevice API
- applications).-function listSuccess(members) -{ - if (members.length === 0) - { - console.log("The archive is empty"); - return; - } - console.log("Files in the archive:") - for (var i=0; i<members.length; i++) - { - console.log(members[i].name); - } +var keyCodes = {}; +var supportedKeys = tizen.inputdevice.getSupportedKeys(); + +console.log("Supported keys list"); +for (var i = 0; i < supportedKeys.length; ++i) { + keyCodes[supportedKeys[i].name] = supportedKeys[i].code; + console.log(i + " : " + supportedKeys[i].name + " - " + supportedKeys[i].code); } -myArchive.getEntries(listSuccess);- -
After the work with the archive is finished, close the archive using the close() method of the ArchiveFile interface.
+Gathered list could be used for handling keydown and keyup events.
--archive.close();-
Creating an Archive
+Gathering information about key
-Creating an archive and adding files to it is a basic archive management skill:
+It is not necessary to gather informations about all supported keys, the getKey() method of the InputDeviceManager interface (in mobile applications). +When list of key is known, each key could be checked separately.
-
-
To create the archive file, use the open() method of the ArchiveManager interface (in mobile and wearable applications) and set the mode as w:
- +-
+
First, it's needed to list of keys, that we are interested.
-tizen.archive.open("downloads/new_archive.zip", "w", createSuccess);
+var keys = ["VolumeUp", "VolumeDown"]; +var keyCodes = {}; +
- - Add a file to the archive using the add() method. The file can be specified using a virtual path:
-
-
-
-function progressCallback(opId, val, name) -{ - console.log("opId: " + opId + " with progress val: " + (val * 100).toFixed(0) + "%"); -} -function successCallback() -{ - console.log("File added"); -} -function createSuccess(archive) -{ - archive.add("downloads/file.txt", successCallback, null, progressCallback); +
Second step is to check result of getKey() call, if it's not null (if null, it would mean that this key is not supported).
+
+for (var i = 0; i < keys.length; i++) { + try { + var key = tizen.inputdevice.getKey(keys[i]); + if (key == null) { + console.log("key: " + keys[i] + " is not supported"); + } else { + keyCodes[key[i].name] = key[i].code; + } + } catch(e) { + console.log("error: " + e.name + ":" + e.message + ", when getting key with name " + keys[i]); + } }
Extracting Files from an Archive
-Extracting a file from an archive is a basic archive management skill:
+Register / unregister input device key
+Changing action of key supported on device is main feature of this module:
-
-
To access an archive file, use the open() method of the ArchiveManager interface (in mobile and wearable applications). The "r" mode is suitable for extracting from the archive.
- --tizen.archive.open("downloads/some_archive.zip", "r", openSuccess, openError);
+To gather supported keys we call code as was mentioned in Getting list of all supported keys.
- - To extract files:
-
-
-
To extract all files from the archive, use the extractAll() method of the ArchiveFile interface (in mobile and wearable applications).
+To register all supported keys for handling keydown and keyup events:
-function progressCallback(opId, val, name) -{ - console.log("extracting operation (: " + opId + ") is in progress (" + (val * 100).toFixed(1) + "%)"); +var codeNamesMap = {}; +var supportedKeys = tizen.inputdevice.getSupportedKeys(); + +for (var i = 0; i < supportedKeys.length; ++i) { + try { + tizen.inputdevice.registerKey(supportedKeys[i].name); + codeNamesMap[supportedKeys[i].code] = supportedKeys[i].name; + console.log("key: " + supportedKeys[i].name + " was registered for event handling"); + } catch(error) { + console.log("failed to register " + supportedKeys[i].name + ": " + error); + } } - -function openSuccess(archive) -{ - archive.extractAll("music", null, null, progressCallback); -}
+
+
-
-
To extract only a selected file from the archive, use the extract() method of the ArchiveFileEntry interface (in mobile and wearable applications).
-First, get the archiveFileEntry object using the getEntryByName() or getEntries() method of the ArchiveFile interface.
+To handle events for registered keys:
++window.addEventListener("keydown", + function(keyEvent) { + if(codeNamesMap.hasOwnProperty(keyEvent.keyCode)) { + console.log("Registered key was pressed"); + //could define some custom action + } else { + console.log("Some other key was pressed"); + } + } +); + +window.addEventListener("keyup", + function(keyEvent) { + if(codeNamesMap.hasOwnProperty(keyEvent.keyCode)) { + console.log("Registered key was released"); + //could define some custom action + } else { + console.log("Some other key was released"); + } + } +); +
+
+ -
+
To unregister keys, when custom actions are no longer needed:
-function extractSuccessCallback() -{ - console.log("File extracted"); -} -function getEntrySuccess(entry) -{ - entry.extract("downloads/extract", extractSuccessCallback); +for (var i = 0; i < supportedKeys.length; ++i) { + tizen.inputdevice.unregisterKey(supportedKeys[i].name); + console.log("key: " + supportedKeys[i].name + " was unregistered for event handling"); } - -function openSuccess(archive) -{ - archive.getEntryByName("my_file.txt", getEntrySuccess); -}
-
-
After unregistration keydown and keyup events would not be triggered for those keys.
-
-