HID Keyboard Demo Problem

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 9:41 am

Already did it but when uncommenting these code it stops working.

Code: Select all

Initialize 
Timer.Items(Timer1).Interval = 1 Timer.Items(Timer1).OnTimer = @OnTimer1 // 1 Ms
Timer.Items(Timer1).Enabled = True
Timer.Start 
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Mon May 09, 2016 10:40 am

What does your OnTimer1 routine look like?

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 11:15 am

Code: Select all

Event OnTimer1()
Inc(Zms)
End Event 

Const Timer1 = 0
"See God, then brake" -Kevin Schwantz

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 11:18 am

using this increment for setting keypress hold times:

Code: Select all

Public Sub Bir(pBas As Integer)
    SendKey(30)
    Repeat Until Zms = pBas
    SendKey(0)
    Zms = 0 Rd
End Sub
"See God, then brake" -Kevin Schwantz

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 11:41 am

Got it.

Changed this:

Code: Select all

Event OnTimer1()
Inc(Zms)
End Event
to this.

Code: Select all

Sub OnTimer1()
Inc(Zms)
End sub
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Mon May 09, 2016 12:03 pm

I guess your SendKey routine only sends the key press info, and not the second key release.
You might try something like this for your Bir routine:

Code: Select all

Public Sub Bir(pBas As word)
    dim tZms as word		// temp copy of Zms
	
    SendKey(30)

    // block isr so event doesn't run while we reset Zms
    Timer.Fblock = true
    Zms = 0 
    tZms = 0
    Timer.Fblock = false

    while (tZms < pBas)
        Timer.Fblock = true		// block isr so Zms doesn't incr while being read since it's a word
        tZms = Zms
        Timer.Fblock = false
    end while

    SendKey(0)
    Rd
End Sub
Completely untested...
Last edited by Jerry Messina on Mon May 09, 2016 2:00 pm, edited 2 times in total.
Reason: correct 'while' loop syntax

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 12:21 pm

Yes, my sendkey does only keypresses. i release key with sendkey(0). My Sub is working but will try yours. Thanks. I will also send key presses in ontimer subroutine.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Mon May 09, 2016 1:24 pm

I fixed the while loop syntax in my previous post. Sorry about that.
The OnTimer1 routine should really be an event, and not a sub.
I will also send key presses in ontimer subroutine
That's probably not a good idea. It might work, but the OnTimer1 routine is called by the timer ISR, and SendKey interacts with the USB interrupt.

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 1:35 pm

When setting it to Event, it won't work. Changing to Sub worked. Weird.

I have to send some keypresses via ontimer, maybe will try to rework it. thanks for helping, i can never fix it myself.
"See God, then brake" -Kevin Schwantz

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Tue May 10, 2016 7:34 am

When sending keypresses from OnTimer Event, code stops working. Removing that code fixes that. Is there a way to send key presses from Timer routine? I really need this.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Tue May 10, 2016 1:40 pm

I can use events, send key press and release messages in timed loops, send keys from timer events, etc. So far, it all seems to work for me.

Maybe you should post exactly what your code is, and what you're trying to do.

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Tue May 10, 2016 3:50 pm

Here is my code. Explained in comment line. (20mhz)
Attachments
xtrabit.zip
(63.25 KiB) Downloaded 165 times
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Tue May 10, 2016 7:19 pm

Code: Select all

Public Sub Iki(pBas As Integer)
    SendKey(31)
    Repeat Until Zms = pBas
    SendKey(0)
    Zms = 0 
    Rd
End Sub
I assume the purpose of this routine is to mimic pressing a key for 'pBas' msecs, and then releasing it?
If so, you'll almost always get stuck in the 'Repeat Until Zms = pBas' loop for about 65 seconds (until Zms wraps around and meets pBas).

When you have this:

Code: Select all

Public Event OnTimer1()
	Inc(Zms)
End Event 
The variable Zms will increase every msec until it eventually wraps around to 0.
If you have 'Repeat Until Zms = pBas' then you'll wait until these two match...if Zms is already > pBas
when this is called, you'll have to wait around awhile.
In addition, you shouldn't try to use a 16-bit value in the ISR event and the main loop without protecting
it from being interrupted.

Also, you can't use Send or SendKey in both the main routine body and the Timer event.
Either send keys in the timer event or send them in main, but not both. The Timer event is called
from the timer ISR, so it's possible that the event will interrupt a SendKey that's already in
process, and this just won't work.

You'll have a lot more luck just letting the timer event track the time and doing the rest in your main loop.

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Wed May 11, 2016 5:45 am

Jerry Messina wrote: I assume the purpose of this routine is to mimic pressing a key for 'pBas' msecs, and then releasing it?
If so, you'll almost always get stuck in the 'Repeat Until Zms = pBas' loop for about 65 seconds (until Zms wraps around and meets pBas).
I set Zms to zero before this, so it counts to my preferred milliseconds.

In addition, you shouldn't try to use a 16-bit value in the ISR event and the main loop without protecting
it from being interrupted.
I will take this into consideration, thanks.
Also, you can't use Send or SendKey in both the main routine body and the Timer event.
Either send keys in the timer event or send them in main, but not both. The Timer event is called
from the timer ISR, so it's possible that the event will interrupt a SendKey that's already in
process, and this just won't work.
That works on 2.1.0.0 version but not on last one. This would help me this code's future variations.
You'll have a lot more luck just letting the timer event track the time and doing the rest in your main loop.
Probably i will have to do this, but last.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Wed May 11, 2016 9:38 am

I'm curious... why do you need to send the keypress info for certain time periods?

Post Reply