USB HID Keyboard

Post here if you want to announce new wiki modules, projects or articles

Moderators: David Barker, Jerry Messina

ta1dr
Registered User
Registered User
Posts: 15
Joined: Tue Aug 18, 2009 8:14 am
Location: istanbul TURKEY

Post by ta1dr » Tue Oct 27, 2009 1:58 pm

Hi Nathan
I used your USB HID Keyboard for my homecocpit (microsoft flight simulator) thanks for share it but I want to know How can I send tree key
example:Ctrl+Shift+a or very popular ctrl+alt+del :wink:

best regards
Ahmet
From TURKEY
I am Licensed User for SF

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Post by RangerBob » Tue Oct 27, 2009 2:41 pm

Hello Ahmet,

Glad you are getting some good use out of this code!

The Modifier keys are special in that they are sent in a different way to the others:

LeftCtrl = %00000001,
LeftShift = %00000010,
LeftAlt = %00000100,
LeftGUI = %00001000,
RightCtrl = %00010000,
RightShift = %00100000,
RightAlt = %01000000,
RightGUI = %10000000

For these you just need to send the OR'ed result of the keys you need in the modifier byte. I.e.

LeftShift = %00000010, LeftAlt = %00000100,

Setting the Modifier byte to %00000110 would send both a left shift and a left alt keypress.
example:Ctrl+Shift+a or very popular ctrl+alt+del
So using the example code you can send this for Ctrl+Shift+a:

Code: Select all

SendKey(a, (LeftShift Or LeftCtrl))
And the good ol'e three fingered salute is:

Code: Select all

SendKey(delete, (LeftCtrl Or LeftAlt))
For non modifier keys you just need to populate the Keyreport.Keyarray bytes. The example Sendkey subroutine does not support this, but you could very easily form the report yourself eg:

Code: Select all

// Send desired key
	KeyReport.Modifier	= none
	KeyReport.Reserved	= 0
	KeyReport.KeyArray0	= a
	KeyReport.KeyArray1	= b
	KeyReport.KeyArray2	= c
	KeyReport.KeyArray3	= d
	KeyReport.KeyArray4	= e
	KeyReport.KeyArray5	= f

Would send keys abcdef all together.

Hope this helps!

Nathan
Last edited by RangerBob on Wed Oct 28, 2009 11:03 am, edited 1 time in total.

ta1dr
Registered User
Registered User
Posts: 15
Joined: Tue Aug 18, 2009 8:14 am
Location: istanbul TURKEY

Post by ta1dr » Tue Oct 27, 2009 4:36 pm

Hello Nathan
many thanks for quickly answer and help

best regards
Ahmet

ta1dr
Registered User
Registered User
Posts: 15
Joined: Tue Aug 18, 2009 8:14 am
Location: istanbul TURKEY

Post by ta1dr » Thu Nov 26, 2009 9:56 pm

I tried Ctrl+Alt+delete it is not work I parse keyboarddefs file delete code is backspace I changed I hope you do not misunderstand me Nathan

Code: Select all

{
*****************************************************************************
*  Name    : KeyboardDefs.BAS                                               *
*  Author  : Nathan Herbert                                                 *
*  Notice  : Copyright (c) 2009                                             *
*          : All Rights Reserved                                            *
*  Date    : 02/02/2009                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Module KeyDefs

//*Definitions*/
Public Const
	a 		= 4,
	b		= 5,		
 	c		= 6,
	d		= 7,
	e		= 8,
	f		= 9,
	g		= 10,
	h		= 11,
	i		= 12,
	j		= 13,
	k		= 14,
	l		= 15,
	m		= 16,
	n		= 17,
	o		= 18,
	p		= 19,
	q		= 20,
	r		= 21,
	s		= 22,
	t		= 23,
	u		= 24,
	v		= 25,
	w		= 26,
	x		= 27,
	y		= 28,
 	z		= 29,
 one		= 30,
 two		= 31,
 three		= 32,
 four		= 33,
 five		= 34,
 six 		= 35,
 seven		= 36,
 eight		= 37,
 nine		= 38,
 zero		= 39,
 enter		= 40,
 escape		= 41,
 bckspace	= 42,  '<------
 tab		= 43,
 spacebar	= 44,
 underscore	= 45,
 equals		= 46,
 leftbrace	= 47,
 rightbrace	= 48,
 backslash	= 49,
 hash		= 50,
 semicolon	= 51,

 comma		= 54,
 period		= 55,
 slash		= 56,
 capslock	= 57,
 f1			= 58,
 f2			= 59,
 f3			= 60,
 f4			= 61,
 f5			= 62,
 f6			= 63,
 f7			= 64,
 f8 		= 65,
 f9			= 66,
 f10		= 67,
 f11		= 68,
 f12		= 69,

 home		= 74,
 pageup		= 75,
 delete     = 76,  '<---- 
 pagedown	= 78,
 rightarrow	= 79,
 leftarrow	= 80,
 downarrow	= 81,
 uparrow	= 82,

// add numpad keys for mouse keys?

// Modifier bytes
None		= %00000000,
LeftCtrl	= %00000001,
LeftShift	= %00000010,
LeftAlt		= %00000100,
LeftGUI		= %00001000,
RightCtrl	= %00010000,
RightShift	= %00100000,
RightAlt	= %01000000,
RightGUI	= %10000000

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Post by RangerBob » Fri Nov 27, 2009 9:07 am

No, I understand, I called the Backspace key "Delete" and hadn't bothered to enter the actual "delete" key. Sorry about that, glad you figured it out. Will change the Wiki to reflect this.

Nathan

mktiger
Posts: 1
Joined: Wed Mar 17, 2010 8:59 pm
Location: Turkiye

Post by mktiger » Wed Mar 17, 2010 9:39 pm

hi RangerBob.
in first place usb device(keyboard) that i done is working properly. but after a period of time it does not work and disconnected from USB automaticly
best regards

Code: Select all

{
*****************************************************************************
*  Name    : USB Keyboard demo.bas                                          *
*  Author  : Nathan Herbert                                                 *
*  Notice  : Copyright (c) 2008                                             *
*          : All Rights Reserved                                            *
*  Date    : 03/02/2009                                                     *
*  Version : 1.1                                                            *
*  Notes   : 1.0 First Revision                                             *
*          : 1.1 Updated to read back LED status                            *
*		   : 1.2 Now opens own wordpad window to show possibile uses		*
*****************************************************************************
}
{ 
Up to 6 individual simultaneous keys can be pressed by using Keyarray 1 through 5
Alts, shift, ctrls etc are handled though modifer bytes. These can be combined by
ORing the required bytes together.
}

Device=18f2550
Clock = 48

// 20Mhz crystal, 48Mhz internal (FS USB)

//Config   PLLDIV = 5
//Config   CPUDIV = OSC1_PLL2
//Config   USBDIV = 2
//Config   FOSC = HSPLL_HS
//Config   VREGEN = ON

Config   MCLRE=OFF
   
#option HID_BUFFER_SIZE = 8
#option USB_DESCRIPTOR = "HIDKeyboardDesc-1.bas"

// Use with FSUSB Bootloader
//#option org_reset = $0800

Include "usbhid.bas"
Include "keyboarddefs.bas"


// Keyboard HID buffer 
Structure TKeyReport
	Modifier	 	As Byte 	// Modifer Byte
	Reserved		As Byte     // Reserved
	KeyArray0		As Byte		// Buttons Pushed
	KeyArray1		As Byte
	KeyArray2		As Byte
	KeyArray3		As Byte
	KeyArray4		As Byte
	KeyArray5		As Byte	
End Structure
Dim KeyReport As TKeyReport Absolute TXReportRAM

// Status LED structure
Structure TLedsReport
	_byte As Byte
	NumLock 		As _byte.0
	capslock 		As _byte.1
	ScrollLock		As _byte.2
	Compose			As _byte.3
	Kana			As _byte.4
	Const1			As _byte.5
	Const2			As _byte.6
	Const3			As _byte.7
End Structure
Dim LedsReport As TLedsReport Absolute RXReportRAM

{Dim 


LED0 As PORTD.0,
LED1 As PORTD.1,
LED3 As PORTD.3
}

{
********************************************************************************
* Name    : SendKey				                                               *
* Purpose : Sends Key Report  				                                   *
********************************************************************************
}
Sub SendKey(pKey As Byte, pModifier As Byte)

	// Send desired key
	KeyReport.Modifier	= pModifier
	KeyReport.Reserved	= 0
	KeyReport.KeyArray0	= pKey
	KeyReport.KeyArray1	= 0
	KeyReport.KeyArray2	= 0
	KeyReport.KeyArray3	= 0
	KeyReport.KeyArray4	= 0
	KeyReport.KeyArray5	= 0

	WriteReport

	// Send Key Release
	KeyReport.Modifier	= 0
	KeyReport.Reserved	= 0
	KeyReport.KeyArray0	= 0
	KeyReport.KeyArray1	= 0
	KeyReport.KeyArray2	= 0
	KeyReport.KeyArray3	= 0
	KeyReport.KeyArray4	= 0
	KeyReport.KeyArray5	= 0

	WriteReport

End Sub

{
********************************************************************************
* Name    : Main				                                               *
* Purpose : Main Routine  				                                   *
********************************************************************************
}
Sub Main()
	DelayMS(100)
While true
    
    If LedsReport.capslock=1 Then
        Select PORTB
            Case %11111110
                SendKey(one,None)
                DelayMS (50)
                SendKey(seven,None)
                DelayMS (50)
                SendKey(eight,None)
                DelayMS (50)
                SendKey(seven,None)
                DelayMS (50)
                SendKey(eight,None)
            Case %11111101
                SendKey(six,None)
                DelayMS (50)
                SendKey(seven,None)
                DelayMS (50)
                SendKey(eight,None)
                DelayMS (50)
                SendKey(seven,None)
                DelayMS (50)
                SendKey(eight,None)
        EndSelect    
	EndIf
			If  HID.DataAvailable Then
				// Bring Report into structure
				ReadReport
				// Set LED 3 to show caps lock status
			EndIf
    Wend	

End Sub

{
********************************************************************************
* Name    : Program Start		                                               *
* Purpose : 				  				                                   *
********************************************************************************
}
{
Low(LED0)
Low(LED1)
Low(LED3)
}
// Wait for USB attach
Repeat
Until HID.Attached

// Attached
//High(LED0)

// Let PC finish enumerating
DelayMS(5000)
INTCON2.7=0
Main

Post Reply