Bus Pirate是一个开源硬件,让开发人员能够直接通过USB和命令行程序和不同的数字,模拟接口的电子模块通讯。

SHT21号称世界上最小的数字式湿度、温度传感器,只有3x3mm大。数字接口和上一代的SHT系列传感器一样使用I2C。

SHT21的电路板可以在这里购买:
http://www.misenso.com/shop/humidity-and-temperature-sensor-sht21-breakout/

SHT21的说明书:
http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf (英文)
http://www.sensirion.com.cn/product/downimg/C-Datasheet_SHT21_V1.0.pdf (中文)

和总线海盗连接

Bus Pirate +3.3V       –>       SHT21 VCC
Bus Pirate   GND       –>       SHT21 GND
Bus Pirate   CLK       –>       SHT21 SCL
Bus Pirate MOSI       –>       SHT21 SDA

操作脚本

HiZ> m   <<< 选择总线类型,因为SHT21是I2C总线
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. KEYB
9. LCD
x. exit(without change)
(1)> 4   <<< 选择I2C总线
Set speed:
1. ~5KHz
2. ~50KHz
3. ~100KHz
4. ~400KHz
(1)> 2   <<< 选择总线时钟频率,根据SHT21手册,SHT21自适应最高400KHz的频率,所以请选择1,2或者3
Ready
I2C> W   <<< 打开3.3v电源
Power supplies ON
I2C> (1)   <<< 搜索连接到I2C总线上的设备地址
Searching I2C address space. Found devices at:
0x80(0x40 W)   <<< 找到SHT21
I2C> [0x80 0xE3][0x81 %:1000 r:3]   <<< 测量温度,读取温度数据
I2C START BIT
WRITE: 0x80 ACK
WRITE: 0xE3 ACK
I2C STOP BIT
I2C START BIT
WRITE: 0x81 ACK
DELAY 1000ms
READ: 0x59  ACK 0x4C  ACK 0x64
NACK
I2C STOP BIT
I2C> [0x80 0xE5][0x81 %:1000 r:3]   <<< 测量湿度,读取湿度数据
I2C START BIT
WRITE: 0x80 ACK
WRITE: 0xE5 ACK
I2C STOP BIT
I2C START BIT
WRITE: 0x81 ACK
DELAY 1000ms
READ: 0x8B  ACK 0x22  ACK 0xDD
NACK
I2C STOP BIT
I2C>

好了,我们获得了温度0x59 0x4C 0x64,湿度0x8B 0x22 0xDD。最后一个字节是校验位,可以忽略。同时,数据最末两位状态位需要被清零,所以我们得到

类型 原始16进制 原始2进制 结果2进制 10进制结果
温度 0x59 0x4C 01011001 01001100 01011001 01001100 22860
湿度 0x8B 0x22 10001011 00100010 10001011 00100000 35616

根据公式

温度= -46.85+175.72*S_T/2^16 =-46.85+175.72*22860/2^16 =14.4435 ℃

相对湿度 = -6+125*  S_RH/2^16 =-6+125*  35616/2^16 =61.932128906%

所以最后我们得到当前温度14.44度,相对湿度61.93%。

总线海盗是一个开源的电子元器件测试与开发工具。总线海盗将常用的电子设备通讯总线集成在一起,方便开发人员快速的测试项目原型。


支持0到5.5伏的多种串行协议:

  • 1-Wire
  • I2C
  • SPI
  • JTAG
  • 异步串行总线 Asynchronous serial
  • MIDI
  • 键盘 PC keyboard
  • HD44780 LCD
  • 2- and 3-wire libraries with bitwise pin control
  • Scriptable binary bitbang, 1-Wire, I2C, SPI, and UART modes

其他功能:

  • 0-6V电压测量
  • 1Hz-40MHz 频率测量
  • 1kHz-4MHz 脉冲宽度调变器,频率产生器
  • 板载多电压上拉电阻
  • 软件可控,板载3.3伏和5伏电压电源
  • 常用功能宏
  • 总线数据监听
  • 设有引导区,方便固件升级
  • 透明USB到串口模式
  • 10Hz-1MHz低速兼容SUMP的逻辑分析器
  • AVR STK500 V2编程器
  • 支持AVRDude编程器
  • 支持Perl、Python等脚本语言

0.1版使用说明:

总线海盗中文说明书0.1

原始docx文档在这里,欢迎转载,协作:http://download.sensorapp.net/总线海盗中文说明书.docx

新出炉的Visual Studio 2010 Express 无法注册,在微软网站提交注册信息之后,就会指向到这么个奇怪的结果地址:

http://wwwstaging/express/Downloads/87W7256K-0T322Q4X-PD0OU915/vcs/vcs2010-pkey_chs.aspx

wwwstaging毫无疑问是不符合域名规范的,绝对是微软自己的bug。说来,微软也不是不知道,但他们在这件事情上极其不认真。在他们内网上也许能访问,但是外网绝对有问题,可惜就没人解决。看看这个bug trace页面,因为用户没有回复,微软就不认账,不修读错误了?

算了,还是得自己手动解决。解决办法很简单,将”wwwstaging”改为万恶的”www.microsoft.com”,再将”chs”改为”enu”。最后你得到类似于这个的链接:

http://www.microsoft.com/express/Downloads/87W7256K-0T322Q4X-PD0OU915/vcs/vcs2010-pkey_enu.aspx

重载页面,你就应该能看到注册码了!

上一篇博客有提到Java实现系统全局键盘鼠标事件监听,但是Java毕竟是Java,老是想着Platform independent,实在是弄出太多的麻烦。要说做Windows桌面应用程序,那还真的只有微软自己当家的.net application最简单好用了。那么,这篇博客主要就是讲C#持续监听你的键盘和鼠标事件。

还是老规矩,直接上代码。

UserActivityHook.cs

[cpp]
using System;

using System.Runtime.InteropServices;

using System.Reflection;

using System.Threading;

using System.Windows.Forms;

using System.ComponentModel;

namespace gma.System.Windows

{

/// <summary>

/// This class allows you to tap keyboard and mouse and / or to detect their activity even when an

/// application runes in background or does not have any user interface at all. This class raises

/// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrive any information you need.

/// </summary>

public class UserActivityHook

{

#region Windows structure definitions

/// <summary>

/// The POINT structure defines the x- and y- coordinates of a point.

/// </summary>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp

/// </remarks>

[StructLayout(LayoutKind.Sequential)]

private class POINT

{

/// <summary>

/// Specifies the x-coordinate of the point.

/// </summary>

public int x;

/// <summary>

/// Specifies the y-coordinate of the point.

/// </summary>

public int y;

}

/// <summary>

/// The MOUSEHOOKSTRUCT structure contains information about a mouse event passed to a WH_MOUSE hook procedure, MouseProc.

/// </summary>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp

/// </remarks>

[StructLayout(LayoutKind.Sequential)]

private class MouseHookStruct

{

/// <summary>

/// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.

/// </summary>

public POINT pt;

/// <summary>

/// Handle to the window that will receive the mouse message corresponding to the mouse event.

/// </summary>

public int hwnd;

/// <summary>

/// Specifies the hit-test value. For a list of hit-test values, see the description of the WM_NCHITTEST message.

/// </summary>

public int wHitTestCode;

/// <summary>

/// Specifies extra information associated with the message.

/// </summary>

public int dwExtraInfo;

}

/// <summary>

/// The MSLLHOOKSTRUCT structure contains information about a low-level keyboard input event.

/// </summary>

[StructLayout(LayoutKind.Sequential)]

private class MouseLLHookStruct

{

/// <summary>

/// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.

/// </summary>

public POINT pt;

/// <summary>

/// If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.

/// The low-order word is reserved. A positive value indicates that the wheel was rotated forward,

/// away from the user; a negative value indicates that the wheel was rotated backward, toward the user.

/// One wheel click is defined as WHEEL_DELTA, which is 120.

///If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,

/// or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,

/// and the low-order word is reserved. This value can be one or more of the following values. Otherwise, mouseData is not used.

///XBUTTON1

///The first X button was pressed or released.

///XBUTTON2

///The second X button was pressed or released.

/// </summary>

public int mouseData;

/// <summary>

/// Specifies the event-injected flag. An application can use the following value to test the mouse flags. Value Purpose

///LLMHF_INJECTED Test the event-injected flag.

///0

///Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0.

///1-15

///Reserved.

/// </summary>

public int flags;

/// <summary>

/// Specifies the time stamp for this message.

/// </summary>

public int time;

/// <summary>

/// Specifies extra information associated with the message.

/// </summary>

public int dwExtraInfo;

}

/// <summary>

/// The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event.

/// </summary>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp

/// </remarks>

[StructLayout(LayoutKind.Sequential)]

private class KeyboardHookStruct

{

/// <summary>

/// Specifies a virtual-key code. The code must be a value in the range 1 to 254.

/// </summary>

public int vkCode;

/// <summary>

/// Specifies a hardware scan code for the key.

/// </summary>

public int scanCode;

/// <summary>

/// Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.

/// </summary>

public int flags;

/// <summary>

/// Specifies the time stamp for this message.

/// </summary>

public int time;

/// <summary>

/// Specifies extra information associated with the message.

/// </summary>

public int dwExtraInfo;

}

#endregion

#region Windows function imports

/// <summary>

/// The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain.

/// You would install a hook procedure to monitor the system for certain types of events. These events

/// are associated either with a specific thread or with all threads in the same desktop as the calling thread.

/// </summary>

/// <param name="idHook">

/// [in] Specifies the type of hook procedure to be installed. This parameter can be one of the following values.

/// </param>

/// <param name="lpfn">

/// [in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a

/// thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link

/// library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process.

/// </param>

/// <param name="hMod">

/// [in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter.

/// The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by

/// the current process and if the hook procedure is within the code associated with the current process.

/// </param>

/// <param name="dwThreadId">

/// [in] Specifies the identifier of the thread with which the hook procedure is to be associated.

/// If this parameter is zero, the hook procedure is associated with all existing threads running in the

/// same desktop as the calling thread.

/// </param>

/// <returns>

/// If the function succeeds, the return value is the handle to the hook procedure.

/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp

/// </remarks>

[DllImport("user32.dll", CharSet = CharSet.Auto,

CallingConvention = CallingConvention.StdCall, SetLastError = true)]

private static extern int SetWindowsHookEx(

int idHook,

HookProc lpfn,

IntPtr hMod,

int dwThreadId);

/// <summary>

/// The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.

/// </summary>

/// <param name="idHook">

/// [in] Handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx.

/// </param>

/// <returns>

/// If the function succeeds, the return value is nonzero.

/// If the function fails, the return value is zero. To get extended error information, call GetLastError.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp

/// </remarks>

[DllImport("user32.dll", CharSet = CharSet.Auto,

CallingConvention = CallingConvention.StdCall, SetLastError = true)]

private static extern int UnhookWindowsHookEx(int idHook);

/// <summary>

/// The CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain.

/// A hook procedure can call this function either before or after processing the hook information.

/// </summary>

/// <param name="idHook">Ignored.</param>

/// <param name="nCode">

/// [in] Specifies the hook code passed to the current hook procedure.

/// The next hook procedure uses this code to determine how to process the hook information.

/// </param>

/// <param name="wParam">

/// [in] Specifies the wParam value passed to the current hook procedure.

/// The meaning of this parameter depends on the type of hook associated with the current hook chain.

/// </param>

/// <param name="lParam">

/// [in] Specifies the lParam value passed to the current hook procedure.

/// The meaning of this parameter depends on the type of hook associated with the current hook chain.

/// </param>

/// <returns>

/// This value is returned by the next hook procedure in the chain.

/// The current hook procedure must also return this value. The meaning of the return value depends on the hook type.

/// For more information, see the descriptions of the individual hook procedures.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp

/// </remarks>

[DllImport("user32.dll", CharSet = CharSet.Auto,

CallingConvention = CallingConvention.StdCall)]

private static extern int CallNextHookEx(

int idHook,

int nCode,

int wParam,

IntPtr lParam);

/// <summary>

/// The CallWndProc hook procedure is an application-defined or library-defined callback

/// function used with the SetWindowsHookEx function. The HOOKPROC type defines a pointer

/// to this callback function. CallWndProc is a placeholder for the application-defined

/// or library-defined function name.

/// </summary>

/// <param name="nCode">

/// [in] Specifies whether the hook procedure must process the message.

/// If nCode is HC_ACTION, the hook procedure must process the message.

/// If nCode is less than zero, the hook procedure must pass the message to the

/// CallNextHookEx function without further processing and must return the

/// value returned by CallNextHookEx.

/// </param>

/// <param name="wParam">

/// [in] Specifies whether the message was sent by the current thread.

/// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.

/// </param>

/// <param name="lParam">

/// [in] Pointer to a CWPSTRUCT structure that contains details about the message.

/// </param>

/// <returns>

/// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.

/// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx

/// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC

/// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook

/// procedure does not call CallNextHookEx, the return value should be zero.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/callwndproc.asp

/// </remarks>

private delegate int HookProc(int nCode, int wParam, IntPtr lParam);

/// <summary>

/// The ToAscii function translates the specified virtual-key code and keyboard

/// state to the corresponding character or characters. The function translates the code

/// using the input language and physical keyboard layout identified by the keyboard layout handle.

/// </summary>

/// <param name="uVirtKey">

/// [in] Specifies the virtual-key code to be translated.

/// </param>

/// <param name="uScanCode">

/// [in] Specifies the hardware scan code of the key to be translated.

/// The high-order bit of this value is set if the key is up (not pressed).

/// </param>

/// <param name="lpbKeyState">

/// [in] Pointer to a 256-byte array that contains the current keyboard state.

/// Each element (byte) in the array contains the state of one key.

/// If the high-order bit of a byte is set, the key is down (pressed).

/// The low bit, if set, indicates that the key is toggled on. In this function,

/// only the toggle bit of the CAPS LOCK key is relevant. The toggle state

/// of the NUM LOCK and SCROLL LOCK keys is ignored.

/// </param>

/// <param name="lpwTransKey">

/// [out] Pointer to the buffer that receives the translated character or characters.

/// </param>

/// <param name="fuState">

/// [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.

/// </param>

/// <returns>

/// If the specified key is a dead key, the return value is negative. Otherwise, it is one of the following values.

/// Value Meaning

/// 0 The specified virtual key has no translation for the current state of the keyboard.

/// 1 One character was copied to the buffer.

/// 2 Two characters were copied to the buffer. This usually happens when a dead-key character

/// (accent or diacritic) stored in the keyboard layout cannot be composed with the specified

/// virtual key to form a single character.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/toascii.asp

/// </remarks>

[DllImport("user32")]

private static extern int ToAscii(

int uVirtKey,

int uScanCode,

byte[] lpbKeyState,

byte[] lpwTransKey,

int fuState);

/// <summary>

/// The GetKeyboardState function copies the status of the 256 virtual keys to the

/// specified buffer.

/// </summary>

/// <param name="pbKeyState">

/// [in] Pointer to a 256-byte array that contains keyboard key states.

/// </param>

/// <returns>

/// If the function succeeds, the return value is nonzero.

/// If the function fails, the return value is zero. To get extended error information, call GetLastError.

/// </returns>

/// <remarks>

/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/toascii.asp

/// </remarks>

[DllImport("user32")]

private static extern int GetKeyboardState(byte[] pbKeyState);

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

private static extern short GetKeyState(int vKey);

#endregion

#region Windows constants

//values from Winuser.h in Microsoft SDK.

/// <summary>

/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.

/// </summary>

private const int WH_MOUSE_LL = 14;

/// <summary>

/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events.

/// </summary>

private const int WH_KEYBOARD_LL = 13;

/// <summary>

/// Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure.

/// </summary>

private const int WH_MOUSE = 7;

/// <summary>

/// Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure.

/// </summary>

private const int WH_KEYBOARD = 2;

/// <summary>

/// The WM_MOUSEMOVE message is posted to a window when the cursor moves.

/// </summary>

private const int WM_MOUSEMOVE = 0x200;

/// <summary>

/// The WM_LBUTTONDOWN message is posted when the user presses the left mouse button

/// </summary>

private const int WM_LBUTTONDOWN = 0x201;

/// <summary>

/// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button

/// </summary>

private const int WM_RBUTTONDOWN = 0x204;

/// <summary>

/// The WM_MBUTTONDOWN message is posted when the user presses the middle mouse button

/// </summary>

private const int WM_MBUTTONDOWN = 0x207;

/// <summary>

/// The WM_LBUTTONUP message is posted when the user releases the left mouse button

/// </summary>

private const int WM_LBUTTONUP = 0x202;

/// <summary>

/// The WM_RBUTTONUP message is posted when the user releases the right mouse button

/// </summary>

private const int WM_RBUTTONUP = 0x205;

/// <summary>

/// The WM_MBUTTONUP message is posted when the user releases the middle mouse button

/// </summary>

private const int WM_MBUTTONUP = 0x208;

/// <summary>

/// The WM_LBUTTONDBLCLK message is posted when the user double-clicks the left mouse button

/// </summary>

private const int WM_LBUTTONDBLCLK = 0x203;

/// <summary>

/// The WM_RBUTTONDBLCLK message is posted when the user double-clicks the right mouse button

/// </summary>

private const int WM_RBUTTONDBLCLK = 0x206;

/// <summary>

/// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button

/// </summary>

private const int WM_MBUTTONDBLCLK = 0x209;

/// <summary>

/// The WM_MOUSEWHEEL message is posted when the user presses the mouse wheel.

/// </summary>

private const int WM_MOUSEWHEEL = 0x020A;

/// <summary>

/// The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem

/// key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.

/// </summary>

private const int WM_KEYDOWN = 0x100;

/// <summary>

/// The WM_KEYUP message is posted to the window with the keyboard focus when a nonsystem

/// key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed,

/// or a keyboard key that is pressed when a window has the keyboard focus.

/// </summary>

private const int WM_KEYUP = 0x101;

/// <summary>

/// The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user

/// presses the F10 key (which activates the menu bar) or holds down the ALT key and then

/// presses another key. It also occurs when no window currently has the keyboard focus;

/// in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that

/// receives the message can distinguish between these two contexts by checking the context

/// code in the lParam parameter.

/// </summary>

private const int WM_SYSKEYDOWN = 0x104;

/// <summary>

/// The WM_SYSKEYUP message is posted to the window with the keyboard focus when the user

/// releases a key that was pressed while the ALT key was held down. It also occurs when no

/// window currently has the keyboard focus; in this case, the WM_SYSKEYUP message is sent

/// to the active window. The window that receives the message can distinguish between

/// these two contexts by checking the context code in the lParam parameter.

/// </summary>

private const int WM_SYSKEYUP = 0x105;

private const byte VK_SHIFT = 0x10;

private const byte VK_CAPITAL = 0x14;

private const byte VK_NUMLOCK = 0x90;

#endregion

/// <summary>

/// Creates an instance of UserActivityHook object and sets mouse and keyboard hooks.

/// </summary>

/// <exception cref="Win32Exception">Any windows problem.</exception>

public UserActivityHook()

{

Start();

}

/// <summary>

/// Creates an instance of UserActivityHook object and installs both or one of mouse and/or keyboard hooks and starts rasing events

/// </summary>

/// <param name="InstallMouseHook"><b>true</b> if mouse events must be monitored</param>

/// <param name="InstallKeyboardHook"><b>true</b> if keyboard events must be monitored</param>

/// <exception cref="Win32Exception">Any windows problem.</exception>

/// <remarks>

/// To create an instance without installing hooks call new UserActivityHook(false, false)

/// </remarks>

public UserActivityHook(bool InstallMouseHook, bool InstallKeyboardHook)

{

Start(InstallMouseHook, InstallKeyboardHook);

}

/// <summary>

/// Destruction.

/// </summary>

~UserActivityHook()

{

//uninstall hooks and do not throw exceptions

Stop(true, true, false);

}

/// <summary>

/// Occurs when the user moves the mouse, presses any mouse button or scrolls the wheel

/// </summary>

public event MouseEventHandler OnMouseActivity;

/// <summary>

/// Occurs when the user presses a key

/// </summary>

public event KeyEventHandler KeyDown;

/// <summary>

/// Occurs when the user presses and releases

/// </summary>

public event KeyPressEventHandler KeyPress;

/// <summary>

/// Occurs when the user releases a key

/// </summary>

public event KeyEventHandler KeyUp;

/// <summary>

/// Stores the handle to the mouse hook procedure.

/// </summary>

private int hMouseHook = 0;

/// <summary>

/// Stores the handle to the keyboard hook procedure.

/// </summary>

private int hKeyboardHook = 0;

/// <summary>

/// Declare MouseHookProcedure as HookProc type.

/// </summary>

private static HookProc MouseHookProcedure;

/// <summary>

/// Declare KeyboardHookProcedure as HookProc type.

/// </summary>

private static HookProc KeyboardHookProcedure;

/// <summary>

/// Installs both mouse and keyboard hooks and starts rasing events

/// </summary>

/// <exception cref="Win32Exception">Any windows problem.</exception>

public void Start()

{

this.Start(true, true);

}

/// <summary>

/// Installs both or one of mouse and/or keyboard hooks and starts rasing events

/// </summary>

/// <param name="InstallMouseHook"><b>true</b> if mouse events must be monitored</param>

/// <param name="InstallKeyboardHook"><b>true</b> if keyboard events must be monitored</param>

/// <exception cref="Win32Exception">Any windows problem.</exception>

public void Start(bool InstallMouseHook, bool InstallKeyboardHook)

{

// install Mouse hook only if it is not installed and must be installed

if (hMouseHook == 0 && InstallMouseHook)

{

// Create an instance of HookProc.

MouseHookProcedure = new HookProc(MouseHookProc);

//install hook

hMouseHook = SetWindowsHookEx(

WH_MOUSE_LL,

MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

//If SetWindowsHookEx fails.

if (hMouseHook == 0)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//do cleanup

Stop(true, false, false);

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

}

// install Keyboard hook only if it is not installed and must be installed

if (hKeyboardHook == 0 && InstallKeyboardHook)

{

// Create an instance of HookProc.

KeyboardHookProcedure = new HookProc(KeyboardHookProc);

//install hook

hKeyboardHook = SetWindowsHookEx(

WH_KEYBOARD_LL,

KeyboardHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

//If SetWindowsHookEx fails.

if (hKeyboardHook == 0)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//do cleanup

Stop(false, true, false);

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

}

}

/// <summary>

/// Stops monitoring both mouse and keyboard events and rasing events.

/// </summary>

/// <exception cref="Win32Exception">Any windows problem.</exception>

public void Stop()

{

this.Stop(true, true, true);

}

/// <summary>

/// Stops monitoring both or one of mouse and/or keyboard events and rasing events.

/// </summary>

/// <param name="UninstallMouseHook"><b>true</b> if mouse hook must be uninstalled</param>

/// <param name="UninstallKeyboardHook"><b>true</b> if keyboard hook must be uninstalled</param>

/// <param name="ThrowExceptions"><b>true</b> if exceptions which occured during uninstalling must be thrown</param>

/// <exception cref="Win32Exception">Any windows problem.</exception>

public void Stop(bool UninstallMouseHook, bool UninstallKeyboardHook, bool ThrowExceptions)

{

//if mouse hook set and must be uninstalled

if (hMouseHook != 0 && UninstallMouseHook)

{

//uninstall hook

int retMouse = UnhookWindowsHookEx(hMouseHook);

//reset invalid handle

hMouseHook = 0;

//if failed and exception must be thrown

if (retMouse == 0 && ThrowExceptions)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

}

//if keyboard hook set and must be uninstalled

if (hKeyboardHook != 0 && UninstallKeyboardHook)

{

//uninstall hook

int retKeyboard = UnhookWindowsHookEx(hKeyboardHook);

//reset invalid handle

hKeyboardHook = 0;

//if failed and exception must be thrown

if (retKeyboard == 0 && ThrowExceptions)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

}

}

/// <summary>

/// A callback function which will be called every time a mouse activity detected.

/// </summary>

/// <param name="nCode">

/// [in] Specifies whether the hook procedure must process the message.

/// If nCode is HC_ACTION, the hook procedure must process the message.

/// If nCode is less than zero, the hook procedure must pass the message to the

/// CallNextHookEx function without further processing and must return the

/// value returned by CallNextHookEx.

/// </param>

/// <param name="wParam">

/// [in] Specifies whether the message was sent by the current thread.

/// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.

/// </param>

/// <param name="lParam">

/// [in] Pointer to a CWPSTRUCT structure that contains details about the message.

/// </param>

/// <returns>

/// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.

/// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx

/// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC

/// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook

/// procedure does not call CallNextHookEx, the return value should be zero.

/// </returns>

private int MouseHookProc(int nCode, int wParam, IntPtr lParam)

{

// if ok and someone listens to our events

if ((nCode >= 0) && (OnMouseActivity != null))

{

//Marshall the data from callback.

MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

//detect button clicked

MouseButtons button = MouseButtons.None;

short mouseDelta = 0;

switch (wParam)

{

case WM_LBUTTONDOWN:

//case WM_LBUTTONUP:

//case WM_LBUTTONDBLCLK:

button = MouseButtons.Left;

break;

case WM_RBUTTONDOWN:

//case WM_RBUTTONUP:

//case WM_RBUTTONDBLCLK:

button = MouseButtons.Right;

break;

case WM_MOUSEWHEEL:

//If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.

//One wheel click is defined as WHEEL_DELTA, which is 120.

//(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value

mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);

//TODO: X BUTTONS (I havent them so was unable to test)

//If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,

//or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,

//and the low-order word is reserved. This value can be one or more of the following values.

//Otherwise, mouseData is not used.

break;

}

//double clicks

int clickCount = 0;

if (button != MouseButtons.None)

if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;

else clickCount = 1;

//generate event

MouseEventArgs e = new MouseEventArgs(

button,

clickCount,

mouseHookStruct.pt.x,

mouseHookStruct.pt.y,

mouseDelta);

//raise it

OnMouseActivity(this, e);

}

//call next hook

return CallNextHookEx(hMouseHook, nCode, wParam, lParam);

}

/// <summary>

/// A callback function which will be called every time a keyboard activity detected.

/// </summary>

/// <param name="nCode">

/// [in] Specifies whether the hook procedure must process the message.

/// If nCode is HC_ACTION, the hook procedure must process the message.

/// If nCode is less than zero, the hook procedure must pass the message to the

/// CallNextHookEx function without further processing and must return the

/// value returned by CallNextHookEx.

/// </param>

/// <param name="wParam">

/// [in] Specifies whether the message was sent by the current thread.

/// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.

/// </param>

/// <param name="lParam">

/// [in] Pointer to a CWPSTRUCT structure that contains details about the message.

/// </param>

/// <returns>

/// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.

/// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx

/// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC

/// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook

/// procedure does not call CallNextHookEx, the return value should be zero.

/// </returns>

private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)

{

//indicates if any of underlaing events set e.Handled flag

bool handled = false;

//it was ok and someone listens to events

if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))

{

//read structure KeyboardHookStruct at lParam

KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

//raise KeyDown

if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))

{

Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

KeyEventArgs e = new KeyEventArgs(keyData);

KeyDown(this, e);

handled = handled || e.Handled;

}

// raise KeyPress

if (KeyPress != null && wParam == WM_KEYDOWN)

{

bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);

bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

byte[] keyState = new byte[256];

GetKeyboardState(keyState);

byte[] inBuffer = new byte[2];

if (ToAscii(MyKeyboardHookStruct.vkCode,

MyKeyboardHookStruct.scanCode,

keyState,

inBuffer,

MyKeyboardHookStruct.flags) == 1)

{

char key = (char)inBuffer[0];

if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);

KeyPressEventArgs e = new KeyPressEventArgs(key);

KeyPress(this, e);

handled = handled || e.Handled;

}

}

// raise KeyUp

if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))

{

Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

KeyEventArgs e = new KeyEventArgs(keyData);

KeyUp(this, e);

handled = handled || e.Handled;

}

}

//if event handled in application do not handoff to other listeners

if (handled)

return 1;

else

return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);

}

}

}
[/cpp]

最后,注册事件,激发事件:

[cpp]

UserActivityHook actHook = new UserActivityHook();

actHook.KeyDown += new KeyEventHandler(MyKeyDown);

actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);

public void MyKeyDown(object sender, KeyEventArgs e)

{

//implement your event handler here

}

public void MouseMoved(object sender, MouseEventArgs e)

{

//implement your event handler here

}
[/cpp]

需要注意的是,请在Visual Studio里面,本项目–>Properties–>Debug里面,取消Enable the Visual Studio hosting process。否则出错。

转载,修改,翻译自 http://www.codeproject.com/KB/cs/globalhook.aspx
原项目上有更多,更详尽内容

标准的Java键盘事件监听器(KeyListener)和鼠标事件监听器(MouseListener)只能在该Java程序聚焦的时候监听事件。要想让你的Java程序能够在系统后台跟踪全局键盘和鼠标事件,那就需要使用JNI(Java Native Interface)来创建一个钩子监听操作系统的事件了。本文只讨论,Java程序与Windows操作系统的交互,如果你知道如何实现Java监听Linux事件,请留言,谢谢。开发运行环境:Windows XP SP3, Java 1.6_15, Eclipse 3.5

直接上代码

SysHook.cpp:

[cpp]

#include <windows.h>
#include "SysHook.h"
#include <jni.h>

HINSTANCE hInst = NULL;

JavaVM * jvm = NULL;
jobject hookObj_kb = NULL;
jobject hookObj_ms = NULL;
jobject g_kl = NULL;

jmethodID processKeyID_kb = NULL;
jmethodID processKeyID_ms = NULL;
DWORD hookThreadId = 0;

LONG    g_mouseLocX = -1;    // x-location of mouse position
LONG    g_mouseLocY = -1;    // y-location of mouse position

extern "C"
BOOL APIENTRY DllMain(HINSTANCE _hInst, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
printf("C++: DllMain – DLL_PROCESS_ATTACH.n");
hInst = _hInst;
break;
default:
break;
}

return TRUE;
}

LRESULT CALLBACK MouseTracker(int nCode, WPARAM wParam, LPARAM lParam)
{
JNIEnv * env;
KBDLLHOOKSTRUCT * p = (KBDLLHOOKSTRUCT *)lParam;

if (jvm->AttachCurrentThread((void **)&env, NULL) >= 0)
{

if (nCode==HC_ACTION)
{
MOUSEHOOKSTRUCT* pStruct = (MOUSEHOOKSTRUCT*)lParam;
if (pStruct->pt.x != g_mouseLocX || pStruct->pt.y != g_mouseLocY)
{
env->CallVoidMethod(hookObj_ms, processKeyID_ms, (jint)pStruct->pt.x,(jint)pStruct->pt.y, g_kl);
g_mouseLocX = pStruct->pt.x;
g_mouseLocY = pStruct->pt.y;
}

}

}
else
{
printf("C++: LowLevelKeyboardProc – Error on the attach current thread.n");
}

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
JNIEnv * env;
KBDLLHOOKSTRUCT * p = (KBDLLHOOKSTRUCT *)lParam;

if (jvm->AttachCurrentThread((void **)&env, NULL) >= 0)
{
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
env->CallVoidMethod(hookObj_kb, processKeyID_kb, (jboolean)TRUE, p->vkCode,g_kl);
break;
case WM_KEYUP:
case WM_SYSKEYUP:
env->CallVoidMethod(hookObj_kb, processKeyID_kb, (jboolean)FALSE, p->vkCode,g_kl);
break;
default:
break;
}
}
else
{
printf("C++: LowLevelKeyboardProc – Error on the attach current thread.n");
}

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

void MsgLoop()
{
MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}

JNIEXPORT void JNICALL Java_SysHook_registerHook(JNIEnv * env, jobject obj,jobject kl)
{
HHOOK hookHandle_ms = SetWindowsHookEx(WH_MOUSE_LL, MouseTracker, hInst, 0);
HHOOK hookHandle_kb = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInst, 0);

g_kl = kl;

if (hookHandle_ms == NULL)
{
printf("C++: Java_SysHook_registerKeyHook – Hook failed!n");
return;
}
else
{
printf("C++: Java_SysHook_registerKeyHook – Hook successfuln");
}

if (hookHandle_kb == NULL)
{
printf("C++: Java_SysHook_registerKeyHook – Hook failed!n");
return;
}
else
{
printf("C++: Java_SysHook_registerKeyHook – Hook successfuln");
}

hookObj_kb = env->NewGlobalRef(obj);
jclass cls_kb = env->GetObjectClass(hookObj_kb);
processKeyID_kb = env->GetMethodID(cls_kb, "processKey", "(ZILGlobalEventListener;)V");

hookObj_ms = env->NewGlobalRef(obj);
jclass cls_ms = env->GetObjectClass(hookObj_ms);
processKeyID_ms = env->GetMethodID(cls_ms, "mouseMoved", "(IILGlobalEventListener;)V");

env->GetJavaVM(&jvm);
hookThreadId = GetCurrentThreadId();

MsgLoop();

if (!UnhookWindowsHookEx(hookHandle_kb))
{
printf("C++: Java_SysHook_registerKeyHook – Unhook failedn");
}
else
{
printf("C++: Java_SysHook_registerKeyHook – Unhook successfuln");
}

if (!UnhookWindowsHookEx(hookHandle_ms))
{
printf("C++: Java_SysHook_registerKeyHook – Unhook failedn");
}
else
{
printf("C++: Java_SysHook_registerKeyHook – Unhook successfuln");
}
}

JNIEXPORT void JNICALL Java_SysHook_unRegisterHook(JNIEnv *env, jobject object)
{
if (hookThreadId == 0)
return;

printf("C++: Java_SysHook_unRegisterKeyHook – call PostThreadMessage.n");
PostThreadMessage(hookThreadId, WM_QUIT, 0, 0L);
}

[/cpp]

SysHook.h:

[cpp]

/* DO NOT EDIT THIS FILE – it is machine generated */
#include <jni.h>
/* Header for class SysHook */

#ifndef _Included_SysHook
#define _Included_SysHook
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     SysHook
* Method:    registerHook
* Signature: (LGlobalEventListener;)V
*/
JNIEXPORT void JNICALL Java_SysHook_registerHook  (JNIEnv *, jobject, jobject);

/*
* Class:     SysHook
* Method:    unRegisterHook
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_SysHook_unRegisterHook  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

[/cpp]

KeyboardEventListener.java

[java]

import java.util.*;

public interface KeyboardEventListener extends EventListener {
public void GlobalKeyPressed(KeyboardEvent event);

public void GlobalKeyReleased(KeyboardEvent event);
}

class KeyboardEvent extends EventObject {
private static final long serialVersionUID = 2341653211621224652L;
boolean ts, ap, ek;
int vk;

public KeyboardEvent(Object source, boolean ts, int vk, boolean ap,
boolean ek) {
super(source);
this.ts = ts;
this.vk = vk;
this.ap = ap;
this.ek = ek;
}

public boolean getTransitionState() {
return ts;
}

public long getVirtualKeyCode() {
return vk;
}

public boolean isAltPressed() {
return ap;
}

public boolean isExtendedKey() {
return ek;
}

public boolean equals(KeyboardEvent event) {
if (event.getVirtualKeyCode() == vk) {
if (event.isExtendedKey() == ek) {
if (event.isAltPressed() == ap) {
return true;
}
}
}
return false;
}
}

[/java]

MouseEventListenter.java

[java]

import java.util.*;

public interface MouseEventListener extends EventListener {
public void GlobalMouseX(MouseEvent event);

public void GlobalMouseY(MouseEvent event);
}

class MouseEvent extends EventObject {

private static final long serialVersionUID = 14654L;
int cord_x, cord_y;

public MouseEvent(Object source, int cord_x, int cord_y) {
super(source);
this.cord_x = cord_x;
this.cord_y = cord_y;
}

public int getMouseX() {
return cord_x;
}

public int getMouseY() {
return cord_y;
}

}

[/java]

GlobalEventListener.java :

[java]

public class GlobalEventListener {
PoolHook pt;

public GlobalEventListener() {
pt = new PoolHook(this);
pt.start();

}

protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();

public void addKeyboardEventListener(KeyboardEventListener listener) {
listenerList.add(KeyboardEventListener.class, listener);
}

public void removeKeyboardEventListener(KeyboardEventListener listener) {
listenerList.remove(KeyboardEventListener.class, listener);
}

public void addMouseEventListener(MouseEventListener listener) {
listenerList.add(MouseEventListener.class, listener);
}

public void removeMouseEventListener(MouseEventListener listener) {
listenerList.remove(MouseEventListener.class, listener);
}

void keyPressed(KeyboardEvent event) {
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i += 2) {
if (listeners[i] == KeyboardEventListener.class) {
((KeyboardEventListener) listeners[i + 1])
.GlobalKeyPressed(event);
}
}
}

void mouseMoved(MouseEvent event) {
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i += 2) {
if (listeners[i] == MouseEventListener.class) {
((MouseEventListener) listeners[i + 1]).GlobalMouseX(event);
((MouseEventListener) listeners[i + 1]).GlobalMouseY(event);
}
}
}

void keyReleased(KeyboardEvent event) {
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i += 2) {
if (listeners[i] == KeyboardEventListener.class) {
((KeyboardEventListener) listeners[i + 1])
.GlobalKeyReleased(event);
}
}
}

}

[/java]

SysHook.java:

[java]

class PoolHook extends Thread {
SysHook hook;
GlobalEventListener g_gl;

PoolHook(GlobalEventListener gl) {
g_gl = gl;
}

public void run() {
hook = new SysHook();
hook.registerHook(g_gl);
}

}

class SysHook {

static {
System.loadLibrary("SysHook");
}

void processKey(boolean ts, int vk, GlobalEventListener gl) {
KeyboardEvent event = new KeyboardEvent(this, ts, vk, false, false);
gl.keyPressed(event);
}

void mouseMoved(int cord_x, int cord_y, GlobalEventListener gl) {
MouseEvent event = new MouseEvent(this, cord_x, cord_y);
gl.mouseMoved(event);
}

native void registerHook(GlobalEventListener gl);

native void unRegisterHook();

}

[/java]

Example.java:

[java]

public class Example implements KeyboardEventListener, MouseEventListener {

static GlobalEventListener gl;

public static void main(String[] args) throws Exception {
Example inst = new Example();
gl = new GlobalEventListener();
gl.addKeyboardEventListener(inst);
gl.addMouseEventListener(inst);
}

@Override
public void GlobalKeyPressed(KeyboardEvent event) {

System.out.println("Key Pressed: " + event.getVirtualKeyCode());
}

@Override
public void GlobalKeyReleased(KeyboardEvent event) {
}

@Override
public void GlobalMouseX(MouseEvent event) {
System.out.println("Mouse X: " + event.getMouseX());

}

@Override
public void GlobalMouseY(MouseEvent event) {
System.out.println("Mouse Y: " + event.getMouseY());
}

}

[/java]

C++文件需要用Visual Studio编译为你的目标系统的DLL文件。如果是标准32位Windows XP,可以在这里下载已编译的文件。

在Eclipse创建工程后,需要做的设置是将该DLL所在目录添加到Native library location如图:

本文摘录、翻译并修改自http://www.jotschi.de/?p=90

Web 技术突飞猛进,Web 设计与开发者们可以选择的工具越来越多,Web 开发者的技巧不再只限于 HTML 和 服务器端编程,还需要精通各种第三方资源,这些第三方资源有时候比你的项目更复杂,更专业,你无法自己实现一切,借助一些 Web API,你可以很方便地将大量优秀的第三方资源集成到自己的站点。本文全面搜集 Web 开发中可能用到的各种第三方资源。

1. 函数与类库 A. CAPTCHA
CAPTCHA 用来防止恶意表单发布,以下 CAPTCHA 系统可以轻松集成到你的程序中:

reCAPTCHA

这是目前最流行的 CAPTCHA 机制,该机制同时是一个古籍数字化工程的一部分,用户在验证的同时,也帮助辨认一些不够清晰的估计扫描。reCAPTCHA 还有一个 Perl 模块 实现该功能。

Securimage

这个一个免费的,开源 PHP CAPTCHA 脚本。

freeCap

基于 GPL 协议的 CAPTCHA 脚本

HN CAPTCHA

PHP CAPTCHA 脚本,基于 LGPL 协议
B. 日期处理
日期操作并不轻松,尽管 PHP 和 Perl 内置了大量此类函数,但未必满足你的需要,以下是几个很好用的日期函数:

  • PEAR Date
    通用 PHP 日期类库
  • Date Class
    PHP 类库,计算及其增减与日期差异
  • C. 图形处理
    图片缩放,添加水印等:

  • Image Manipulation Class
    在 PHP 中缩放,反转,旋转图片
  • PHP Thumbnailer
    一个轻量级图 片缩略图工具
  • D. 表单验证
    表单验证不仅保证用户填写的准确,还可以防止攻击:

  • validaForms
    一 个 PHP 表单验证类库
  • User Input Validation Class
    一个 PHP 表单验证基础库
  • E. 密码验证
    验证密码的复杂度

  • Strength Test
    一 个 PHP 密码强度验证函数

  • 2. Ajax 与 JavaScript A. 自动输入建议
    最早最成熟的自 动输入建议应该是 Google 搜索条。

  • Facebook-Style Autosuggest
    Facebook 风格的自动输入建议
  • Autosuggest / Autocomplete with Ajax
    基于 XML 和 JSON 数据源的自动输入建议工具
  • Ajax.Autocompleter
    基 于 script.aculo.us 的输入自动完成工具
  • B. 日历
    在输入日期的地方,使用非常直观的日历面板

  • Vista-Like Ajax Calendar
    基于 Mootools,Vista 风格
  • JS Calendar in DHTML Suite
    基于 JavaScript ,支持多日历连接

  • C. 滑动条
    滑动条可以更直观地进行数值的输入和选择

  • Accessible Slider
    一个设计出众的滑动条控件
  • Phatfusion Slider
    一 个基于 AJAX 的滑动条
  • AJAXEngine Sliders
    AJAXEngine 项目中的滑动条
  • D. 表格
    Ajax 表格,支持排序等功能

  • Grid3
    作 为 Ext JS 库一部分的表格控件
  • dhtmlxGrid
    支 持列排序和搜索
  • Unobtrusive Table Sort Script
    支持多列排序
  • AJAX Data Listings Engine
    基于 PHP 和 MySQL
  • TableKit
    基 于 Prototype JavaScript 框架
  • E. 可拖动内容

  • Draggable Content Scripts
    dhtmlgoodies.com 推出多种可拖放内容脚本,以及图片裁剪工具
  • DragResize
    支 持尺寸修改功能

  • F. 图片放大
    类似 JavaScript 灯箱的图片放大显示控件

  • Highslide JS
    在 Modal 窗口显示放大尺寸的图片
  • FancyZoom
    图 片缩放显示
  • TJPzoom 3
    图片放大工具

  • G. 相册和幻灯
    用相册或幻灯方式显示系列图片

  • minishowcase
    基于 AJAX 和 JSON 的相册控件
  • Animated JavaScript Slideshow
    轻量级 JavaScript 图片幻灯效果,支持运动字幕
  • Hoverbox Image Gallery
    简单的相册,鼠标在缩略图上移动时显示原图
  • H. 打分工具
    非常直观的打分控件

  • Starbox
    基 于 Prototype JavaScript 框架
  • Unobtrusive AJAX Star Rating Bar
    基于 PHP 和 AJAX
  • CSS: Star Rater Ajax Version
    基于 AJAX
  • I. 取色板
    用于取色

  • jscolor
    简单的弹出式取色板
  • JavaScript Color Picker
    支持 RGB,HSL,以及16进制图片色值
  • Tigra Color Picker
    非常简单的取色板
  • J. 进度条

  • jsProgressBarHandler
    非 常灵活
  • YUI Loading Panel Widget
    非常漂亮.
  • Simple Javascript Progress Bar with CSS
    简单实用.
  • Uploadify
    基于 jQuery.3. APIs
    以下 API 可以将很多现成的功能集成到你的站点:

    4. IP 定位
    用户来自什么地方。

    • IP Details
      一个 PHP 类库,根据 IP 地址获取用户地理位置
    • GEO-IP
      IP 到国家对应数据库

    5. 图表

    • amCharts
      基于 Flash,支持 3D 图表

    6. 地图

    • amMap
      交互式地图工具,支持下钻式挖掘

    • Flashmaps
      多个基于 Flash 的地图工具

    7. 音频播放器

    8. 视频播放器

    • OS FLV
      开源 Flash 视频播放器

    9. 视频转换

    10. 所见即所得编辑器

    • TinyMCE
      一个轻量的,基于 JavaScript 的所见即所得编辑器

    • XINHA
      功能完整的开源 WYSIWYG 编辑器

    • NicEdit
      一个简单的轻量级 WYSIWYG 编辑器
    • openWYSIWYG
      开源,跨浏览器 WYSIWYG 编辑器
    • CKEditor
      设计出众,功能丰富的 WYSIWYG 编辑器
  • 转载自http://www.cnbeta.com/articles/106525.htm http://www.cnbeta.com/articles/106527.htm
    来源 http://www.noupe.com/tools/100-essential-web-development-tools.html

    status.net原名Laconica (读作“拉框已昆”),是一个免费开源的微博平台。帮助社区,公司和团队进行网上短信息(140个字)交流。用户可以选择跟随他喜欢的朋友或同事,收取他们的消息更新。Laconica提供与Twitter, Jaiku和Plurk相似的服务。
    在Windows下安装Laconica很简单,下面让我们一步一步来做:

    1. 下载并解压缩XAMPP程序到本地硬盘
       下载地址:http://www.apachefriends.org/download.php?xampplite-win32-1.7.2.zip
       根据线报,此版本包含的PHP 5.3.0与Joomla有冲突!如需使用Joomla,请使用老版本XAMPP Lite
       注意XAMPP只能用来玩,不要用来当作真正的服务器
    xamppFolder

    2. 双击setup_xampp.bat,安装XAMPP

    3. 为了能运行status.net(Laconica),我们还要对XAMPP做一些简单的设置。首先,打开XAMPPapachebin下的php.ini文件,删除extension=php_curl.dll前面的那一个分号后保存文件

    4. 双击xampp_start.exe开始运行XAMPP网络服务器。注意,你应该可以看到这么一个窗口,请不要关闭该窗口!
    xamppStart
    如果发现窗口一闪而过就消失了,说明你机器的80端口被占用了,必须关闭占用该端口的程序,再运行xampp_start.exe。具体方法是运行xampp-portcheck.exe,找到占用80端口的程序,关闭它。如下图,skype占用了80端口,必须关闭skype才能正常启动XAMPP
    xamppSkype

    5. 在浏览器打开http://localhost/security/index.php,为MySQL数据库root用户添加新密码,填入123456,如图设置,点击更改密码
    xmappSecurity

    6. 在浏览器输入http://localhost/phpmyadmin/,用户名root,密码123456。登录后创建一个叫做locanica的数据库,如图

    phpmyadmin

    7.  下载解压缩status.net(Laconica),并存放到XAMPP/htdocs文件夹里面,完成后如下图
         Laconica下载地址 http://laconi.ca/trac/
    laconicaInhtdocs

    8. 在浏览器输入http://localhost/laconica/install.php或者status.net的目录名,填入下图所示配置
    installPHP

    9. 点击Submit后,应该可以看到下图
    installComplete

    10. 在浏览器输入http://localhost/laconica/,如果看到下图,你就可以慢慢玩了~~
    laconicadone

    11. 能够运行php程序的Web hosting服务都应该支持Loconica,安装起来大同小异,就不再讲了。如果遇到问题,请你留言。请随意转载,注明出处,谢谢。

    Tomasz Kobialka, Rajkumar Buyya, Peng Deng, Lars Kulik, Marimuthu Palaniswami, Sensor Web: Integration of Sensor Networks with Web and Cyber Infrastructure, Handbook of Research on Developments and Trends in Wireless Sensor Networks: From Principle to Practice, IGI Global, USA, 2009. (in press, accepted on March 3, 2009)

    SensorWeb ProjectSensorWeb3.0 Releasenotes   SensorWeb3.0 Releasenotes pauldeng

    今天早上,在再一次通读全文,修改部分语法、表达之后,讲毕业设计报告提交给了导师。接下来面对的就是紧张的考试了~~

    依然按照我的风格,文档代码全部开源,除非特别说明所有资源全部使用GPL v3发放,请遵守该License的规定。

    ABSTRACT: We developed a gesture based human computer interaction interface. Wireless sensor node is used to capture human body acceleration data. To segment received acceleration data stream, we developed an algorithm based on sliding window and standard deviation. To recognize gesture, Hidden Markov Model (HMM) which is a machine learning algorithm is used. Series prototype applications are built to demonstrate possible gesture based applications in future. We conducted several experiments as well. Finally, we got highest 96% accuracy and lowest 17% accuracy.

    KEYWORDS: body sensor network, wireless sensor network, data stream processing, hidden markov model, machine learning, gesture recognition, human-computer interface

     

    (非IE用户可能不能正常浏览)

    程序源代码将在考试之后放到网上,谢谢。


    同时,将本科毕业设计报告一并开源。巧合的是,本科毕业设计完成时间6月10日,研究生毕业设计完成时间6月11日。大家看看两年了,是否有进步。

     

    (非IE用户可能不能正常浏览)

    程序源代码将在考试之后放到网上,谢谢。

    其实毕业设计的正式名字还没想好,反正就是捕获人体动作的加速度,识别人在做什么动作,基本就是这样。

    Demo搞完了,这几天准备继续赶制论文……

     

    (非IE用户可能不能正常查看幻灯)