Click here to Skip to main content
Click here to Skip to main content

Screen Keyboard

By , 23 Jun 2005
Rate this:
Please Sign up or sign in to vote.

Introduction

The screen keyboard can write all the fields (password field, address bar, Notepad etc.). You can use Ctrl, Shift, Alt, Altgr, Esc, (Alt + f4) or any other keyboard events. You can also use the screen keyboard for opening Notepad, WordPad, Winamp, Outlook, Internet Explorer, Task Manager, CDROM tray, calculator, run window, search, media player and your Hotmail inbox. The program shows your MSN messenger contact list along with the names of all the songs you have listened to, simultaneously. The screen keyboard looks like Microsoft screen keyboard, but it has some extra properties.

Background

My project heavily uses Win32 API (Application Programming Interface) .The screen keyboard contains low level language properties. I have seen similar samples written in C++, but they cannot write every where. I wanted the screen keyboard to be written in VB.NET because I could not find any code here in CodeProject or any other site that fulfilled my requirements. Almost all other similar programs were written in C++, and none of them fully met my requirements. The API text viewer contains all the Windows API functions and constraints. The API text viewer also displays all your requirements when you use it.

Windows API references

  • File, Memory, Process, Threading, Time, Console and Comm control(kernel32.dll)
  • Windows handling, and Windows UI control(user32.dll)
  • Graphics and Imaging(gdi32.dll)
  • Audio, Video, and Joystick control (winmm.dll)
  • Registry, Event Log, Authentication, and Services(advapi32.dll)
  • Printing (winspoll.dll)
  • Asian characters support(imm32.dll)
  • Executing processes(shell32.dll)
  • Winsock, Windows berkley socket support(wsock32.dll)
  • WNet* Instrumentation (mpr.dll)
  • Common Dialog control(comdlg32.dll)
  • Windows Network support(netapi32.dll)
  • Windows Compression(lz32.dll)
  • Common Controls(comctl32.dll)
  • Versioning support (version.dll)
  • Object linking and embedding(ole32.dll)

Screen keyboard

API functions for screen keyboard

Public Declare Function SetActiveWindow Lib "user32" Alias _
                   "SetActiveWindow"(ByVal hwnd As Long) As Long

Declare Function keybd_event Lib "user32" Alias "keybd_event" _
     (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, _
     ByVal dwExtraInfo As Long) As Long

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
     (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
     ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) _
     As Integer

API constraints for screen keyboard

Example
Const KEYEVENTF_KEYUP = &H2 ' Release key
Const VK_RETURN = &HD 'enter
Const VK_SHIFT = &H10 'Shift
Const VK_CONTROL = &H11 'Ctrl
Const VK_CAPITAL = &H14 'capslock
Const VK_ESCAPE = &H1B 'escape
etc.

Screen keyboard has 122 constraints. &HD, H10, &H1B etc. are the key’s hexadecimal codes.

Screen keyboard functions

Public Function whichbuttonpushed(ByVal _
                       sender As String) As String

The sender string gets the button's name when you click the button.

Example
Select Case _sender
    Case "btna" 
        keybd_event(VK_A, 0, 0, 0) 'send a key 
        keybd_event(VK_A, 0, KEYEVENTF_KEYUP, 0)'release a key 
        shiftrelease() 'release shift 
        altrelease() 'release alt 
        leftaltrelease() 'release altgr
    Case "btnb"
        keybd_event(VK_B, 0, 0, 0) 'send b key
        keybd_event(VK_B, 0, KEYEVENTF_KEYUP, 0)'release b key
        shiftrelease() 'release shift 
        altrelease() 'release alt 
        leftaltrelease() 'release altgr 
        etc.
end select

The following function gets the numlockbutton names and controls the numlock key state:

Public Function numlockfunc(ByVal numlock_key As String, _
                      ByVal open_close As Boolean) As String
Example
If open_close = False Then 'numlock opened
    Select Case numlock_key
        Case "btnnumlockdiv"
            keybd_event(VK_DIVIDE, 0, 0, 0)
            keybd_event(VK_DIVIDE, 0, KEYEVENTF_KEYUP, 0)
            etc.
    End Select
Else 'numlock closed
    Select Case numlock_key
        Case "btnnumlockdiv"
            keybd_event(VK_DIVIDE, 0, 0, 0)
            keybd_event(VK_DIVIDE, 0, KEYEVENTF_KEYUP, 0)
            etc. 
    End Select 
End If

Screen keyboard set focus function

The keyboard sets the focus to the active window when the button is clicked. The active window introducer number is (8975651603260375040). I found this number using the GetActiveWindow function:

Public Declare Function GetActiveWindow Lib "user32" _
                      Alias "GetActiveWindow" () As Long

The following code snippet is very important for the screen keyboard because the function SetActiveWindow sets the focus to the active window.

Example
SetActiveWindow(8975651603260375040)

The SetActiveWindow function is used after the button is clicked.

Messenger API

.NET contains Interop.MessengerAPI. I have included Interop.MessengerAPI.dll as reference and used the singin method. In addition to this MessengerAPI contains the openinbox property. Openinbox enables us to go to the Hotmail inbox directly if any one signs in on the computer.

Get the Winamp process

Dim processes() As process
Dim process As New process
Dim processstring As String
Sub Timer1_Tick
Dim mycoll As New Collection
processes = process.GetProcesses
For Each process In processes
    mycoll.Add(process, process.Id.ToString)
    If process.ProcessName = "winamp" Then
        processstring = process.MainWindowTitle 
        'gets song’s name(MainWindowTitle)
        Exit For
    End If
Next
Me.Text = "MY SCREEN KEYBOARD" & Space(55) & _
DateTime.Now & Space(20) & processstring
End sub

Capture the screen

Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
System.Threading.Thread.Sleep(200) 
' To have time to catch the clipboard
Static i As Integer
Dim data As IDataObject
data = Nothing
data = Clipboard.GetDataObject()
Dim bmap As Bitmap
Dim p As New PictureBox
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), 
                                                        Bitmap)
    p.Image = bmap
    i += 1
    p.Image.Save(
      "C:\Documents and Settings\All Users\Desktop\Screenshot " _
                  & i.ToString & " .jpg", Imaging.ImageFormat.Jpeg)
End If
If Not File.Exists(
      "C:\Documents and Settings\All Users\Desktop\Screenshot " _
                                       & i.ToString & " .jpg") Then
    screenshot()
Else
End If

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Magician_Oz
Web Developer
Turkey Turkey
No Biography provided

Comments and Discussions

 
QuestionI need help to port web based G7 onscreen-keypad to Windows Native application. PinmemberMember 815507611-Aug-12 1:22 
Question?? Pinmemberlobotomy10-Apr-10 1:29 
Generala simple question Pinmemberdanceoften14-Dec-09 3:15 
GeneralPInvokeStackImbalance was detected Pinmemberchabdesign21-Feb-07 5:11 
GeneralRe: PInvokeStackImbalance was detected PinmemberEduardo Carstensen19-Jul-13 8:51 
QuestionError Message While Trying To Run This Code PinmemberFaruk Ansari6-Dec-06 7:58 
AnswerRe: Error Message While Trying To Run This Code PinmemberBob Beaubien18-Sep-09 5:22 
AnswerRe: Error Message While Trying To Run This Code PinmemberMarktw021-Nov-09 18:36 
GeneralProblem in Web Browser Control Pinmemberrajansarna14-Nov-06 20:55 
QuestionKeys do not press Pinmembercowman8-Sep-06 15:12 
AnswerRe: Keys do not press PinmemberKakkaduz17-Oct-07 23:49 
GeneralRe: Keys do not press Pinmemberketnoibanbe26-May-09 23:58 
GeneralError with ComboBox in DropDown Style PinmemberChili55523-Nov-05 23:37 
GeneralRe: Error with ComboBox in DropDown Style Pinmemberpoman318-Sep-10 2:13 
GeneralSetActiveWindow(8975651603260375040) Pinmemberavnersimon1-Oct-05 7:59 
QuestionRe: SetActiveWindow(8975651603260375040) PinmemberDic197822-Apr-10 4:43 
Generalreading the Pressed key PinmemberAmir Jalaly2-Jul-05 20:58 
GeneralBloq Num PinmemberVitoto2-Jul-05 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 23 Jun 2005
Article Copyright 2005 by Magician_Oz
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid