Page 1 of 1

USB PIC18F2550 Keyboard/HID query

Posted: Mon Aug 03, 2015 6:36 am
by Aresby
I've now got the PIC18F4550 USB Keyboard/HID project working just fine thanks to you guys but I have a query.

The SendKey function seems to continue sending the last "keystroke" unless you send a "0" (zero) - is this normal? Does it continue to send the "0" or does it in fact stop sending anything (which is what I want).

To clarify, if I send the characters "H","E","L","L","O" then stop sending the "O" continues to be sent until I send a "0" (zero). Is this just the way the module is written or a "feature" of USB/HID communications - and can I do anything about it?

Any ideas?

Re: USB PIC18F2550 Keyboard/HID query

Posted: Thu Aug 13, 2015 10:46 am
by RangerBob
The SendKey function should look like this:

Code: Select all

Sub SendKey(pKey As Byte, pModifier As Byte = None)
	
	// 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() //<----- THIS REPORT WILL CONTAIN THE DESIRED KEYPRESS(ES)
	
	// 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()  //<----- THIS REPORT WILL CONTAIN THE RELEASE
	
End Sub
The HID keyboard relies upon press and release notification reports. This allows the OS to handle keyrepeats by itself, with the keyboard merely reporting whether a key has been pressed or released. Our "Keypress" function emulates this by doing two reports in succession, the first being the press, the second being the release. If you repeatedly hit the function, or leave out the release report, the OS will keep spewing characters. If you could post some code which produces this behaviour we could take a look at it.

Regards,

Re: USB PIC18F2550 Keyboard/HID query

Posted: Thu Aug 13, 2015 5:01 pm
by Aresby
Ah ha! <Eureka Moment>

That means that if I just send "A", "B" and "C" it means (to the OS) that those keys have been pressed but never released. I'm guessing that the OS then just recognises the last key pressed which is why it continues to be "seen" as part of the OS's key repeat function - it's not being continually sent from the microcontroller at all.

Perhaps I should be sending the sequence "A", "0", "B", "0", "C", "0" to emulate a proper key press and release sequence?

Re: USB PIC18F2550 Keyboard/HID query

Posted: Fri Aug 14, 2015 8:20 am
by RangerBob
Absolutely correct. If there isn't a release report the OS will keep spewing character. But if you look at the "Sendkey" subroutine posted above, thats exactly what happens. So to send a sequence "A,B,C,D" you would just need to call (using the KeyDefs module for the constants):

Code: Select all

SendKey(a, LeftShift)
SendKey(b, LeftShift)
SendKey(c, LeftShift)
SendKey(d, leftShift)
for lower case ("a,b,c,d"), don't send the shift modifier.

Code: Select all

SendKey(a)
SendKey(b)
SendKey(c)
SendKey(d)
The SendKey sub always sends an empty report after sending the desired key. If you have some example code to look at, that might help spot the problem.