{"id":28590,"date":"2013-06-27T14:29:44","date_gmt":"2013-06-27T08:59:44","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=28590"},"modified":"2024-04-12T14:16:11","modified_gmt":"2024-04-12T08:46:11","slug":"keyboard-events","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/","title":{"rendered":"Keyboard events"},"content":{"rendered":"<p><a class=\"vsc\" href=\"http:\/\/www.vskills.in\/certification\/Certified Blackberry Apps Developer\"><span class=\"vsc-cn\" style=\"text-align: center;\"><span style=\"color: red;\">Certify and Increase Opportunity.<\/span><br \/>\n<span style=\"color: green;\">Be <\/span><br \/>\nGovt. Certified Blackberry Apps Developer<br \/>\n<\/span><\/a><\/p>\n<h1>Using the keyboard<\/h1>\n<div>\n<div id=\"_donesection_4D83ACC407DE42669DEA3D23809390E313394424455873\">\n<p>The event handling framework for BlackBerry 10 is BlackBerry Platform Services (BPS). This framework allows you to register for and receive events from the underlying OS. This includes useful things such as the virtual keyboard, screen, and device sensors. We&#8217;ll use the BPS library to set up the virtual keyboard.<\/p>\n<p>To use any virtual keyboard function, we&#8217;ll need to include the appropriate header file at the beginning of main.c:<\/p>\n<pre>#include &lt;bps\/virtualkeyboard.h&gt;<\/pre>\n<p>This application draws a colored square on the screen and uses OpenGL ES to update and render the graphics. The Glview library provided with the BlackBerry 10 Native SDK makes it easy to develop apps with OpenGL ES. It provides an execution loop as well as API functions for an application to register callbacks at different points of execution. We&#8217;ll use Glview to set up and run the application. To do this, we include the glview header file:<\/p>\n<pre>#include &lt;glview\/glview.h&gt;<\/pre>\n<p>In\u00a0main(), all we need to do is register three callback functions with Glview and then callglview_loop().<\/p>\n<pre>int\r\nmain(int argc, char *argv[])\r\n{\r\n    glview_initialize(GLVIEW_API_OPENGLES_11, &amp;render);\r\n    glview_register_initialize_callback(&amp;initialize);\r\n    glview_register_event_callback(&amp;event);\r\n    return glview_loop();\r\n}<\/pre>\n<p>Now let&#8217;s take a closer look at the callbacks.<\/p>\n<\/div>\n<div>\n<h2 id=\"initialization_callback\">Initialization callback<\/h2>\n<p>We register the initialization callback to initialize the square and display the virtual keyboard. This callback is called before Glview enters the main loop.<\/p>\n<pre>glview_register_initialize_callback(&amp;initialize);<\/pre>\n<p>In\u00a0initialize(), we first request virtual keyboard events so that we&#8217;ll know when someone presses a key.<\/p>\n<pre>virtualkeyboard_request_events(0);<\/pre>\n<p>Next, we display the virtual keyboard on the screen:<\/p>\n<pre>virtualkeyboard_show();<\/pre>\n<p>The initialization code then uses calls to OpenGL ES functions to set up the display area, the background color and smooth shading, and initialize a simple orthographic projection for rendering:<\/p>\n<pre>unsigned int surface_width, surface_height;\r\nglview_get_size(&amp;surface_width, &amp;surface_height);\r\n\r\nglShadeModel(GL_SMOOTH);\r\n\r\nglClearColor(1.0f, 1.0f, 1.0f, 1.0f);\r\n\r\nglViewport(0, 0, surface_width, surface_height);\r\nglMatrixMode(GL_PROJECTION);\r\nglLoadIdentity();\r\n\r\nglOrthof(0.0f, (float) (surface_width) \/ (float) (surface_height), 0.0f,\r\n         1.0f, -1.0f, 1.0f);\r\n\r\nglMatrixMode(GL_MODELVIEW);\r\nglLoadIdentity();\r\n\r\nglTranslatef((float) (surface_width) \/ (float) (surface_height) \/ 2, 0.5f,\r\n             0.0f);<\/pre>\n<\/div>\n<div>\n<h2 id=\"display_callback\">Display callback<\/h2>\n<p>We need to register a callback to render the square. This callback is mandatory for using Glview and is called frequently to refresh the graphics to ensure the smooth rotation of the square.<\/p>\n<p>Our display callback is\u00a0render()\u00a0and we register it using\u00a0glview_initialize(). We also specify the use of OpenGL ES version 1.1:<\/p>\n<pre>glview_initialize(GLVIEW_API_OPENGLES_11, &amp;render);<\/pre>\n<p>In order to draw the square, we initialize an array of vertices and an array of colors for the vertices at the beginning of main.c:<\/p>\n<pre>static const GLfloat vertices[] = {\r\n    -0.25f, -0.25f,\r\n     0.25f, -0.25f,\r\n    -0.25f,  0.25f,\r\n     0.25f,  0.25f\r\n};\r\n\r\nstatic const GLfloat colors[] = {\r\n    1.0f, 0.0f, 1.0f, 1.0f,\r\n    1.0f, 1.0f, 0.0f, 1.0f,\r\n    0.0f, 1.0f, 1.0f, 1.0f,\r\n    0.0f, 1.0f, 1.0f, 1.0f\r\n};<\/pre>\n<p>We also define a variable\u00a0angle, which denotes the rotation angle for the square:<\/p>\n<pre>static float angle = 0.0;<\/pre>\n<p>Our callback must first call\u00a0glClear()\u00a0to clear the color buffer:<\/p>\n<pre>glClear(GL_COLOR_BUFFER_BIT);<\/pre>\n<p>Then it enables the\u00a0GL_VERTEX_ARRAY\u00a0state and provides an array of vertices that describe our simple square:<\/p>\n<pre>glEnableClientState(GL_VERTEX_ARRAY);\r\nglVertexPointer(2, GL_FLOAT, 0, vertices);<\/pre>\n<p>Similarly, it enables the\u00a0GL_COLOR_ARRAY\u00a0state and provides an array that defines the color of each of our vertices:<\/p>\n<pre>glEnableClientState(GL_COLOR_ARRAY);\r\nglColorPointer(4, GL_FLOAT, 0, colors);<\/pre>\n<p>Now it adds a rotation with respect to the vertical axis and then renders the square. The rotation angle has been initialized to 0 so that when the square is rendered initially, it doesn&#8217;t rotate. As we&#8217;ll see in the event callback code, the value of\u00a0angle\u00a0will change according to the keyboard input we receive.<\/p>\n<pre>glRotatef(angle, 0.0f, 1.0f, 0.0f);\r\nglDrawArrays(GL_TRIANGLE_STRIP, 0 , 4);<\/pre>\n<p>Finally, we disable all client states that were used by the current rendering logic as it&#8217;s generally a good practice to do so. This simple step can save you a lot of time in front of the debugger as EGL code is typically quite difficult to debug.<\/p>\n<pre>glDisableClientState(GL_VERTEX_ARRAY);\r\nglDisableClientState(GL_COLOR_ARRAY);<\/pre>\n<\/div>\n<div>\n<h2 id=\"event_callback\">Event callback<\/h2>\n<p>To handle user input from the screen, we register an event callback, in which we check each incoming event and respond to it. When a user presses a key, for example, a screen event is generated and Glview invokes the event callback we register.<\/p>\n<pre>glview_register_event_callback(&amp;event);<\/pre>\n<p>The event callback handles two types of events: virtual keyboard events and screen events. When receiving a virtual keyboard event, it sets the\u00a0keyboard_visible\u00a0flag accordingly. It checks this flag later to determine whether to display the virtual keyboard.<\/p>\n<pre>if (virtualkeyboard_get_domain() == domain) {\r\n    switch (code) {\r\n    case VIRTUALKEYBOARD_EVENT_VISIBLE:\r\n        keyboard_visible = true;\r\n        break;\r\n    case VIRTUALKEYBOARD_EVENT_HIDDEN:\r\n        keyboard_visible = false;\r\n        break;\r\n    }\r\n}<\/pre>\n<p>When a user touches anywhere on the screen or presses a key, a screen event is sent to the application. \u00a0On detecting a touch on the screen, we display the virtual keyboard if it isn&#8217;t already visible.<\/p>\n<pre>switch (screen_val) {\r\n    case SCREEN_EVENT_MTOUCH_TOUCH:\r\n        if (!keyboard_visible) {\r\n            virtualkeyboard_show();\r\n        }\r\n    break;<\/pre>\n<p>When a key is pressed, we receive a keyboard event and perform the desired action. For example, we can change the layout of the keyboard so that it&#8217;s more suited to some function the user wants to perform, such as sending an email or entering a phone number.<\/p>\n<pre>case SCREEN_EVENT_KEYBOARD:\r\n    screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_KEY_FLAGS, &amp;screen_val);\r\n\r\n    if (screen_val &amp; KEY_DOWN) {\r\n        screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_KEY_SYM,&amp;screen_val);\r\n\r\n        fprintf(stderr, \"The '%c' key was pressed\\n\", (char)screen_val);\r\n\r\n        switch (screen_val) {\r\n        case KEYCODE_I:\r\n            \/\/ Display the email layout with \"Send\" enter key\r\n            virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_EMAIL, VIRTUALKEYBOARD_ENTER_SEND);\r\n            break;\r\n        case KEYCODE_O:\r\n            \/\/ Display the phone layout with \"Connect\" enter key\r\n            virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_PHONE, VIRTUALKEYBOARD_ENTER_CONNECT);\r\n            break;\r\n        case KEYCODE_P:\r\n            \/\/ Display the default layout with default enter key\r\n            virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_DEFAULT, VIRTUALKEYBOARD_ENTER_DEFAULT);\r\n            break;<\/pre>\n<div class=\"apply\">\n<h3>Apply for Blackberry Apps Certification Now!!<\/h3>\n<p><a href=\"http:\/\/www.vskills.in\/certification\/Certified-Blackberry-Apps-Developer\">http:\/\/www.vskills.in\/certification\/Certified-Blackberry-Apps-Developer<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Certify and Increase Opportunity. Be Govt. Certified Blackberry Apps Developer Using the keyboard The event handling framework for BlackBerry 10 is BlackBerry Platform Services (BPS). This framework allows you to register for and receive events from the underlying OS. This includes useful things such as the virtual keyboard, screen, and device sensors. We&#8217;ll use the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[3346],"tags":[4364],"class_list":["post-28590","page","type-page","status-publish","hentry","category-blackberry-apps","tag-keyboard-events"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Keyboard events - Tutorial<\/title>\n<meta name=\"description\" content=\"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keyboard events - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:46:11+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/\",\"name\":\"Keyboard events - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-06-27T08:59:44+00:00\",\"dateModified\":\"2024-04-12T08:46:11+00:00\",\"description\":\"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Keyboard events\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Keyboard events - Tutorial","description":"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/","og_locale":"en_US","og_type":"article","og_title":"Keyboard events - Tutorial","og_description":"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:46:11+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/","name":"Keyboard events - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-06-27T08:59:44+00:00","dateModified":"2024-04-12T08:46:11+00:00","description":"Keyboard events. Government of India Certification in Blackberry Apps Development. Get certified and improve employability.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/keyboard-events\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Keyboard events"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/28590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=28590"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/28590\/revisions"}],"predecessor-version":[{"id":64856,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/28590\/revisions\/64856"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=28590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=28590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=28590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}