USB HID Keyboard
Moderators: David Barker, Jerry Messina
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.
And the good ol'e three fingered salute is:
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:
Would send keys abcdef all together.
Hope this helps!
Nathan
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.
So using the example code you can send this for Ctrl+Shift+a:example:Ctrl+Shift+a or very popular ctrl+alt+del
Code: Select all
SendKey(a, (LeftShift Or LeftCtrl))
Code: Select all
SendKey(delete, (LeftCtrl Or LeftAlt))
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
Hope this helps!
Nathan
Last edited by RangerBob on Wed Oct 28, 2009 11:03 am, edited 1 time in total.
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
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
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