Virtual keyboard driver

Source: Internet
Author: User

 

This is a problem that I wanted to solve a long time ago.
In the past, buttons on the Development Board were not really similar to the USB keyboard, and cannot be used as input for graphical interfaces such as QT.
According to eldd, we finally talked about the input subsystem.


It can be seen that it is unreasonable to directly read/write/dev/input/the device nodes in the directory, and I can only think of this in the past.
Http://blog.sina.com.cn/s/blog_4673bfa50100b7jf.html
However, it is even more disgusting to read the examples provided by the source code document after reading the eldd for a long time.
Or get the implementation instance from the http://blog.csdn.net/absurd/archive/2009/09/13/4549514.aspx
Driver code:
# Include <Linux/module. h>
# Include <Linux/moduleparam. h>
# Include <Linux/init. h>
# Include <Linux/kernel. h>
# Include <Linux/slab. h>
# Include <Linux/fs. h>
# Include <Linux/errno. h>
# Include <Linux/types. h>
# Include <Linux/proc_fs.h>
# Include <Linux/fcntl. h>
# Include <Linux/AIO. h>
# Include <ASM/uaccess. h>
# Include <Linux/IOCTL. h>
# Include <Linux/cdev. h>
# Include <Linux/input. h>
Int vkeyboard_major = 201;
Module_param (vkeyboard_major, Int, 0 );
Module_author ("Li xianjing <xianjimli@hotmail.com> ");
Module_license ("GPL ");

Static struct input_dev * vkeyboard_idev = NULL;
Static int vkeyboard_input_dev_setup (void );
Int vkeyboard_open (struct inode * inode, struct file * filp)
{
Return 0;
}

Int vkeyboard_release (struct inode * inode, struct file * filp)
{
Return 0;
}

Ssize_t vkeyboard_read (struct file * filp, char _ User * Buf, size_t count, loff_t * f_pos)
{
Printk (kern_info "% s \ n", _ FUNC __);
Return count;
}

Struct keyboard_event
{
Int press;
Int key;
};

Ssize_t vkeyboard_write (struct file * filp, const char _ User * Buf, size_t count, loff_t * f_pos)
{
Int ret = 0;
Struct keyboard_event event;

While (Ret <count)
{
If (copy_from_user (& event, BUF + ret, sizeof (event )))
{
Return-efault;
}
RET + = sizeof (event );

Input_event (vkeyboard_idev, ev_msc, msc_scan, event. Key );
Input_report_key (vkeyboard_idev, event. Key, event. Press );
Input_sync (vkeyboard_idev );

Printk (kern_info "% s p = % d key = % d with scan Code \ n", _ FUNC __, event. Press, event. Key );
}

Return ret;
}

Static struct file_operations vkeyboard_fops =
{
. Owner = this_module,
. Open = vkeyboard_open,
. Release = vkeyboard_release,
. Read = vkeyboard_read,
. Write = vkeyboard_write,
};

Static int _ init vkeyboard_init (void)
{
Int result = register_chrdev (vkeyboard_major, "vkeyboard", & vkeyboard_fops );
Vkeyboard_input_dev_setup ();

Return result;
}

Static void _ exit vkeyboard_cleanup (void)
{
Input_unregister_device (vkeyboard_idev );
Unregister_chrdev (vkeyboard_major, "vkeyboard ");

Return;
}

Module_init (vkeyboard_init );
Module_exit (vkeyboard_cleanup );

Static int vkeyboard_input_dev_open (struct input_dev * IDEV)
{
Printk (kern_info "% s \ n", _ FUNC __);

Return 0;
}

Static void vkeyboard_input_dev_close (struct input_dev * IDEV)
{
Printk (kern_info "% s \ n", _ FUNC __);

Return;
}

Static int vkeyboard_input_dev_setup (void)
{
Int ret = 0;
Int I = 0;
Vkeyboard_idev = input_allocate_device ();

If (vkeyboard_idev = NULL)
{
Return-enomem;
}

Vkeyboard_idev-> evbit [0] = bit_mask (ev_key) | bit_mask (ev_abs) | bit_mask (ev_msc );
Vkeyboard_idev-> mscbit [0] = bit_mask (msc_scan) | bit_mask (msc_serial) | bit_mask (msc_raw );

Bitmap_fill (vkeyboard_idev-> keybit, key_max );
Bitmap_fill (vkeyboard_idev-> relbit, rel_max );
Bitmap_fill (vkeyboard_idev-> absbit, abs_max );

Vkeyboard_idev-> name = "vkeyboard ";
Vkeyboard_idev-> phys = "vkeyboard/input0 ";
Vkeyboard_idev-> open = vkeyboard_input_dev_open;
Vkeyboard_idev-> close = vkeyboard_input_dev_close;

For (I = 32; I <key_max; I ++)
{
Input_set_capability (vkeyboard_idev, ev_key, I );
}
_ Set_bit (ev_key, vkeyboard_idev-> evbit );

Ret = input_register_device (vkeyboard_idev );

Return ret;
}

Client code

# Include <fcntl. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <errno. h>

# Include <Linux/input. h>

# Define device_name "/dev/vkeyboard"

Struct keyboard_event
{
Int press;
Int key;
};

Int main (void)
{
Struct keyboard_event event;
Int FD;
Int I = 0;
Event. Key = key_up;
FD = open (device_name, o_rdwr | o_ndelay );
If (FD <0)
{
Perror ("Open error ");
Exit (-1 );
}
While (1)
{
// Read (FD, event, 1 );
Event. Press = 1;
Write (FD, & event, sizeof (event ));
Event. Press = 0;
Write (FD, & event, sizeof (event ));
Sleep (1 );

// Sleep (1 );
Printf ("% d \ n", I ++ );
}
Close (FD );
Return 0;
}

Enter an up arrow.
What do you need? Search for it in/Linux/input. h.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.