Writing a keyboard driver
Results 1 to 8 of 8

Thread: Writing a keyboard driver

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351

    Question Writing a keyboard driver

    Okay, I found a simple tutorial on writing a keyboard driver: http://www.geocities.com/dev_das_2k/...rd_driver.html

    The code:

    Code:
    #include <stdio.h>
    #include <dos.h>
    #include <conio.h>
    
    #define INTR 0X09            /*The interrupt number for keyboard*/
    
    /*These r scan codes of non-displayable keys*/
    #define NIL        -1
    #define ESC       27
    #define CTRL    29
    #define SHFT    42
    #define ALT       56
    #define CAPS    58
    #define F1         59
    #define F2         60
    #define F3         61
    #define F4         62
    #define F5         63
    #define F6         64
    #define F7         65
    #define F8         66
    #define F9         67
    #define F10       68
    #define NUM     69
    #define SCRL    70
    #define HOME   71
    #define UP        72
    #define PGUP    73
    #define LEFT    75
    #define FIVE     76
    #define RGHT   77
    #define END      79
    #define DOWN  80
    #define PGDN   81
    #define INS       82
    #define DEL      83
    #define F11      87
    #define F12      88
    #define WIN     91
    #define RTCK  93
    #define POWR  94
    #define SLEP   95
    #define WAKE  99
    
    /*The scancode-to-ASCII table*/
    
    char ascii[]= {
    
        NIL, ESC, '1', '2', '3', '4', '5', '6', '7', '8',
        '9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r',
        't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', CTRL,
        'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
        '\'', '`', SHFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n',
        'm', ',', '.', '/', SHFT, '*', ALT, ' ', CAPS, F1,
        F2, F3, F4, F5, F6, F7, F8, F9, F10, NUM,
        SCRL,HOME, UP, PGUP, '-', LEFT, FIVE, RGHT, '+', END,
        DOWN, PGDN, INS, DEL, NIL, NIL, NIL, F11, F12, NIL,
        NIL, WIN, WIN, RTCK, POWR, SLEP, NIL, NIL, NIL, WAKE
    };
    
    
    /*function pointer to save the old interrupt handler*/
    void interrupt ( *oldhandler)(...); 
    
    /*our new interrupt handler. When u press a key, this function gets called*/
    void interrupt handler(...)
    {
        unsigned char scan_code = inportb(0x60);     /*read scancode byte from port 0x60*/
    
        /* when the key is pressed,the MSB is cleared so check for this condition*/
        if(!(scan_code & 0x80)) 
            printf("%c",ascii[scan_code]);
    
        outportb(0x20,0x20);     /*acknowledge PIC that we have received interrupt*/
        oldhandler();                 /*call the old handler to do its work*/
    }
    
    void main()
    {
        /*save the old interrupt vector*/
        oldhandler = getvect(INTR);
    
        /*install the new interrupt handler*/
        setvect(INTR, handler);
    
        /*A delay of 10 seconds. What ever u type in this period,will be displayed by our new interrupt handler*/
        delay(10000);
    
        /*reset the old interrupt handler*/
        setvect(INTR, oldhandler);
      }
    Alright, now I understand all the code, and I use Dev-C++. Do I just open it and hit "Compile"...and rename the EXE to VXD? I'd have to write an INF to install it..but is there anything else to it....would it work in Windows?

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    This piece of code looks like a DOS program to me (getvect, setvect,
    hardware interrupt 0x09). Reminds me of the good old TSR time
    What is the operating system you are running?

    With Windows 95[1], VXD (virtual device driver) has been introduced.
    Have a look at Techsoft's VxDWriter[2] for a beginning and download
    the linked DDK. Usually, these drivers are part in the [386Enh] section
    of the system.ini file.

    Starting with Windows 98, the WDM[3] (Windows Driver Model) became
    standard. The best (multipart) tutorial is on thecodeproject.com[4].

    Cheers

    [1] http://www.microsoft.com/technet/arc.../rk18_cfg.mspx
    [2] http://www.techsoftpl.com/vxd/
    [3] http://msdn.microsoft.com/library/de...6f32be.xml.asp
    [4] http://www.thecodeproject.com/system/driverdev.asp
    http://www.thecodeproject.com/system/driverdev2.asp
    http://www.thecodeproject.com/win32/driverdev3.asp
    http://www.thecodeproject.com/system/driverdev4asp.asp
    http://www.codeproject.com/system/driverdev5asp.asp
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    So I can use a TSR compiler to compile this as EXE or COM? I'd then rename it to .DRV and copy it to a DOS floppy?

    Any TSR compilers you reccommend?

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    A TSR (Terminate and Stay Resident) is a special executable. You don't need a special compiler for it.

    But it's a really, really old skool way of running multiple programs from the MS-DOS era when there was no multitasking.. I doubt it'll work properly with win9x and later..

    There's loads of info on MSDN on writing drivers for windows.. You're probably going to need the DDK (Driver Development Kit).. Have a look at that to.. IIRC it comes with lots of documentation and some examples for you to play with...
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    Well, how would I go about compiling this as TSR? I use Dev-C++ 5. Also, after I compile it, it's going to leave me with keyboard.exe, can I just change the extension to .DRV and try it out? Maybe I can find an old-school DOS diskette lying around and try it out in there...


    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  6. #6
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    Update: I've tried compiling with Dev-C++. Here's the output and all the errors.

    Compiler: Default compiler
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp" -o "C:\Documents and Settings\AxessTerminated\Desktop\driver.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:63: error: variable or field `interrupt' declared void
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:63: error: `oldhandler' was not declared in this scope
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:63: error: expected `,' or `;' before '(' token
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:66: error: `interrupt' does not name a type
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:79: error: `main' must return `int'
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp: In function `int main(...)':

    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:81: error: `oldhandler' undeclared (first use this function)
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:81: error: (Each undeclared identifier is reported only once for each function it appears in.)
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:81: error: `getvect' undeclared (first use this function)

    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:84: error: `handler' undeclared (first use this function)
    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:84: error: `setvect' undeclared (first use this function)

    C:\Documents and Settings\AxessTerminated\Desktop\driver.cpp:87: error: `delay' undeclared (first use this function)

    Execution terminated
    Geek isn't just a four-letter word; it's a six-figure income.

  7. #7
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    AxessTerminated, tell us what you want to accomplish (... )
    based on which operating system, and some of us might be able to help
    you to continue. Writing drivers can be quite a complex and specific task
    to perform. Maybe you just have to look into GetAsyncKeyState[1] ?

    If you really want to - you might be able to use this particular piece of
    code by starting an emulator and/or by booting a PC with an old dos disk.
    In addition, you need a 16bit compiler (e.g. Turbo-C[2]), which
    knows the keyword interrupt. Obviously, Dev-C++ does not know it,
    and hence, the whole compilation aborts.

    interrupt

    The interrupt keyword was a keyword to modify the calling method of
    a function. All registers were pushed to the stack prior to execution -
    in the end, all registers were popd from the stack, the return was
    given by IRET. As SirDice pointed out: this is quite dated,
    and nowadays, obsolete in standard systems.

    Again, as mentioned by myself an SirDice - do some reading first
    and have a look at the DDKs[3].

    Cheers.

    /edit:
    Nostalgia: Just checked the web for a Turbo-C compiler. I have found
    the version 2.0, for which I have the reference book

    [1] http://msdn.microsoft.com/library/de...nckeystate.asp
    [2] http://bdn.borland.com/article/0,1410,20841,00.html
    [3] http://www.microsoft.com/whdc/devtools/ddk/default.mspx
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  8. #8
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    I want to be able to compile this, try it out, make changes...I've always wanted to get into Linux driver development...but I'm still a bit new to C++, so I'm learning with DOS first.


    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •