USB PIC18F2550 Keyboard/HID query

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
Aresby
Posts: 64
Joined: Fri Mar 16, 2012 8:35 am
Location: Milton Keynes, UK
Contact:

USB PIC18F2550 Keyboard/HID query

Post by Aresby » Mon Aug 03, 2015 6:36 am

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?
Aresby
Swordfish & PIC Newbie

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

Re: USB PIC18F2550 Keyboard/HID query

Post by RangerBob » Thu Aug 13, 2015 10:46 am

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,

Aresby
Posts: 64
Joined: Fri Mar 16, 2012 8:35 am
Location: Milton Keynes, UK
Contact:

Re: USB PIC18F2550 Keyboard/HID query

Post by Aresby » Thu Aug 13, 2015 5:01 pm

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?
Aresby
Swordfish & PIC Newbie

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

Re: USB PIC18F2550 Keyboard/HID query

Post by RangerBob » Fri Aug 14, 2015 8:20 am

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.

Post Reply