Detecting a Touchscreen in Visual Basic

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction

In this day and age, it is critical to know on which platform your application is run. Today, I will show you how to check if your application is running on a touchable device.

Detecting Touch

There are a few ways to detect touch events in your non-UWP applications. Non-UWP applications are not Universal apps. UWP has its own ways and means of identifying which devices your applications are run on. I am talking about ordinary Windows applications here. I will demonstrate two methods of detecting a touchscreen in your program.

With both methods that I will show you today, one Windows API is involved. This API is named GetSystemMetrics. The GetSystemMetrics API function retrieves various system metrics (widths and heights of Windows display elements) and system configuration settings. System metrics are the dimensions (widths and heights) of Windows display elements. All dimensions retrieved by GetSystemMetrics are in pixels.

Our Project

Design your project as per Figure 1.

Our Design
Figure 1: Our Design

Add the GetSystemMetrics API with its associated Constants:

   Public Declare Auto Function GetSystemMetrics Lib _
      "user32.dll" (ByVal smIndex As Integer) As Integer

   Const SM_MAXIMUMTOUCHES As Integer = 95
   Const SM_DIGITIZER As Integer = 94
   Const NID_READY As Integer = 128

SM_MAXIMUMTOUCHES returns the maximum number of contacts supported by each digitizer in the system. If the system has only single-touch digitizers, the return value will be 1. If the system has multi-touch digitizers, the return value is the number of simultaneous contacts the hardware can provide.

The SM_DIGITIZER setting specifies the type of digitizers that are installed on a device. NID_READY identifies if the device is ready to receive digitizer input.

Add the two functions that check if you have a touch-enabled screen:

   Public Function TouchScreen1() As Boolean

      Dim intTouch As Integer

      intTouch = GetSystemMetrics(SM_MAXIMUMTOUCHES)

      Return intTouch > 0

   End Function

   Private Function TouchScreen2() As Boolean

      Return GetSystemMetrics(SM_DIGITIZER) And NID_READY

   End Function

The TouchScreen1 Function returns the number of touch-enabled devices. The TouchScreen2 function returns true or false, depending on whether or not your device is touch-enabled.

The following code creates an onscreen keyboard. I decided to include it in this project because touchscreens are quite common in kiosks. Add the CreateKeyboard sub:

   Private Sub CreateKeyBoard()

      Dim btnAlpha As Button

      Dim intLeft As Integer = 10
      Dim intTop As Integer = 60

      For i As Integer = Asc("A") To Asc("Z")

         ' The following code creates the alphabetic '
         ' keypad, as shown in Figure 2 '
         btnAlpha = New Button

         With btnAlpha

            .Name = "btnAlpha_" & Chr(i)
            .Text = "&" & Chr(i)

            .Size = New Size(25, 25)
            .Location = New Point(intLeft, intTop)

            .Tag = Chr(i)

            intLeft = intLeft + 20

            If Chr(i) = "M" Then

               intTop = intTop + 30
               intLeft = 10

            End If

            .Visible = True

         End With

         Me.Controls.Add(btnAlpha)

      Next

      Dim btnNum As Button

      intLeft = 10
      intTop = 60

      For j As Integer = 0 To 9

         ' The following code creates the numeric '
         ' keypad, as shown in Figure 3 '
         btnNum = New Button

         With btnNum

            .Name = "btnNum_" & j
            .Text = "&" & j

            .Size = New Size(25, 25)
            .Location = New Point(intLeft, intTop)

            .Tag = j

            intLeft = intLeft + 20

            .Visible = True

         End With

         Me.Controls.Add(btnNum)

      Next

   End Sub

Letters
Figure 2: Letters

Numbers
Figure 3: Numbers

First, I created a new button object named btnAlpha. This button or buttons will be placed on the form and will hold all the letters of the alphabet. Next, I created two objects to hold the first button's location (in pixels), and later on I will manipulate the left and top objects to put the buttons next to each other. I then made use of a with structure to set the dynamic button's Name, Text, Size, Location, and Tag properties inside the For loop.

When the character M is reached, the characters must continue under the previous 13 buttons. I then set the Visible property to true, added the dynamic button to the form, and created an event handler for when the particular button is clicked. Lastly, I added the number buttons. I won't go into too many details here because I have written a more complete and more advanced Onscreen keyboard, which you are welcome to look at.

Add the last event:

   Private Sub Form1_Load(sender As Object, _
         e As EventArgs) Handles Me.Load

      If TouchScreen1() Then

         CreateKeyBoard()

      End If

      If TouchScreen2() Then

         CreateKeyBoard()

      End If

   End Sub

You do not have to run both tests. The preceding code in the Form_Load event is just an example of the implementation of both methods.

Download the Code

Below this article, you'll find a link to download the code that accompanies this article. Please feel free to use it.

Conclusion

Detecting touchscreens is an important trick to know, especially today, when there are so many different Windows Operating Systems and devices.



About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date