Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / Visual Basic
Article

Screen Keyboard

Rate me:
Please Sign up or sign in to vote.
4.40/5 (30 votes)
23 Jun 20052 min read 196.3K   13K   79   18
Win32 API

Image 1

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

VB
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
VB
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

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

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

Example
VB
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:

VB
Public Function numlockfunc(ByVal numlock_key As String, _
                      ByVal open_close As Boolean) As String
Example
VB
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:

VB
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
VB
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

VB
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

VB
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


Written By
Web Developer
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI need help to port web based G7 onscreen-keypad to Windows Native application. Pin
Member 815507611-Aug-12 1:22
Member 815507611-Aug-12 1:22 
Question?? Pin
lobotomy10-Apr-10 1:29
professionallobotomy10-Apr-10 1:29 
Dead | X| bu ne amuğa goim :S
inverse of dog

Generala simple question Pin
danceoften14-Dec-09 3:15
danceoften14-Dec-09 3:15 
GeneralPInvokeStackImbalance was detected Pin
chabdesign21-Feb-07 5:11
chabdesign21-Feb-07 5:11 
GeneralRe: PInvokeStackImbalance was detected Pin
Eduardo Carstensen19-Jul-13 8:51
Eduardo Carstensen19-Jul-13 8:51 
QuestionError Message While Trying To Run This Code Pin
Faruk Ansari6-Dec-06 7:58
Faruk Ansari6-Dec-06 7:58 
AnswerRe: Error Message While Trying To Run This Code Pin
Bob Beaubien18-Sep-09 5:22
Bob Beaubien18-Sep-09 5:22 
AnswerRe: Error Message While Trying To Run This Code Pin
Marktw021-Nov-09 18:36
Marktw021-Nov-09 18:36 
GeneralProblem in Web Browser Control Pin
rajansarna14-Nov-06 20:55
rajansarna14-Nov-06 20:55 
QuestionKeys do not press Pin
cowman8-Sep-06 15:12
cowman8-Sep-06 15:12 
AnswerRe: Keys do not press Pin
Kakkaduz17-Oct-07 23:49
Kakkaduz17-Oct-07 23:49 
GeneralRe: Keys do not press Pin
ketnoibanbe26-May-09 23:58
ketnoibanbe26-May-09 23:58 
GeneralError with ComboBox in DropDown Style Pin
Chili55523-Nov-05 23:37
Chili55523-Nov-05 23:37 
GeneralRe: Error with ComboBox in DropDown Style Pin
poman318-Sep-10 2:13
poman318-Sep-10 2:13 
GeneralSetActiveWindow(8975651603260375040) Pin
avnersimon1-Oct-05 7:59
avnersimon1-Oct-05 7:59 
QuestionRe: SetActiveWindow(8975651603260375040) Pin
Dic197822-Apr-10 4:43
Dic197822-Apr-10 4:43 
Generalreading the Pressed key Pin
Amir Jalaly2-Jul-05 20:58
Amir Jalaly2-Jul-05 20:58 
GeneralBloq Num Pin
Vitoto2-Jul-05 16:00
Vitoto2-Jul-05 16:00 

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

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