?? typematic.cpp
字號:
////////////////////////////////////////////////////////////////
// MSDN Magazine -- January 2005
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// Typematic shows how to use SendInput to send keystrokes to another app.
// Typematic lets you define abbreviations and then type them automatically
// into a form or any app by pressing <Win>+T followed by the one-letter
// abbreviation. For example, <Win>+T followed by n to enter your name. Also
// shows how to register a hot key to activate your app.
//
#include "stdafx.h"
#include "resource.h"
#include "TraceWin.h"
#include "StatLink.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const HOTKEY = 'T'; // <WinKey>+T is hotkey to activate
static void SendString(LPCTSTR str);
//////////////////
// Override static text control to process keyboard input.
// Normally, static control doesn't handle input.
//
class CStaticAbbrev : public CStatic {
protected:
afx_msg UINT OnGetDlgCode()
{
return DLGC_WANTCHARS; // I want all chars
}
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
};
//////////////////
// Main window/dialog
//
class CMyDialog : public CDialog {
public:
CMyDialog(CWnd* pParent = NULL);
~CMyDialog();
protected:
CStatic m_wndHelp; // help message
CButton m_wndOK; // OK/Exit button
CStaticAbbrev m_wndKeys;// static text to display/receive abbreviations
CStaticLink m_wndLink1; // web links...
CStaticLink m_wndLink2;
CStaticLink m_wndLink3;
UINT m_nIDHotKey; // hot key identifier
BOOL m_bFirstTime; // first time activated?
virtual BOOL OnInitDialog();
virtual void OnOK();
virtual void OnCancel();
afx_msg LRESULT OnHotKey(WPARAM wp, LPARAM lp);
DECLARE_MESSAGE_MAP()
};
//////////////////
// Generic MFC application object
//
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance() {
CMyDialog dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE; // done
}
} theApp;
//////////////////
// Table of abbreviations. Edit to supply your own info.
// In a real app you should read from user-specific file/settings.
//
struct ABBREV {
TCHAR key;
LPCTSTR text;
} MYABBREVS[] = {
{ _T('n'),_T("Elmer Fudd") }, // name
{ _T('a'),_T("1 Bunny Way") }, // addr
{ _T('c'),_T("Redmond") }, // city
{ _T('p'),_T("206 555 1212") }, // phone
{ 0,NULL},
};
////////////////////////////////////////////////////////////////
// CStaticAbbrev
BEGIN_MESSAGE_MAP(CStaticAbbrev, CStatic)
ON_WM_GETDLGCODE()
ON_WM_CHAR()
END_MESSAGE_MAP()
//////////////////
// Static control got a character: look for it in abbrev table.
// If found, hide the dialog and send the text. Note that whatever window had
// focus before activating will have focus again after hiding, so the right
// window gets the text. If you are trying to send to a specific app, you
// should make sure it's the foreground window before calling SendInput.
//
void CStaticAbbrev::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
for (int i=0; MYABBREVS[i].key; i++) {
if (nChar==MYABBREVS[i].key) { // found:
GetParent()->ShowWindow(SW_HIDE); // ..hide dialog
SendString(MYABBREVS[i].text); // ..and send text
return;
}
}
MessageBeep(0);
}
////////////////////////////////////////////////////////////////
// CMyDialog
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_MESSAGE(WM_HOTKEY, OnHotKey)
END_MESSAGE_MAP()
CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/) : CDialog(IDD_MYDIALOG, pParent)
{
m_bFirstTime = TRUE;
}
//////////////////
// Dialog destroyed: unregister hot key.
//
CMyDialog::~CMyDialog()
{
UnregisterHotKey(m_hWnd, m_nIDHotKey);
}
//////////////////
// Dialog just created: initialize it
//
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// subclass controls
m_wndKeys.SubclassDlgItem(IDC_STRINGS, this);
m_wndHelp.SubclassDlgItem(IDC_HELPMSG, this);
m_wndOK.SubclassDlgItem(IDOK, this);
m_wndLink1.SubclassDlgItem(IDC_PDURL, this);
m_wndLink2.SubclassDlgItem(IDC_MSDNURL, this);
m_wndLink3.SubclassDlgItem(IDC_MSDNMAG, this);
// Register "Windows-T" as my hot key (Windows key + T)
m_nIDHotKey = GlobalAddAtom("TypematicHotkey");
RegisterHotKey(m_hWnd, m_nIDHotKey, MOD_WIN, HOTKEY);
return FALSE; // return TRUE unless you set the focus to a control
}
//////////////////
// User pressed OK (first time) or Exit button.
//
void CMyDialog::OnOK()
{
if (m_bFirstTime)
ShowWindow(SW_HIDE); // don't quit, just hide
else
EndDialog(0);
}
//////////////////
// User pressed Escape: don't quit unless first time.
//
void CMyDialog::OnCancel()
{
if (m_bFirstTime)
EndDialog(0); // first time: cancel as normal
else
ShowWindow(SW_HIDE); // don't quit dialog, just hide
}
//////////////////
// User pressed hot key: activate myself.
//
LRESULT CMyDialog::OnHotKey(WPARAM wp, LPARAM lp)
{
if (wp==m_nIDHotKey && !IsWindowVisible()) {
if (m_bFirstTime) {
// first activation: replace inital hello message with abbrevs list
CString text;
for (int i=0; MYABBREVS[i].key; i++) {
CString temp;
temp.Format(_T("%c = %s\n"), MYABBREVS[i].key, MYABBREVS[i].text);
text += temp;
}
m_wndKeys.SetWindowText(text);
m_wndHelp.SetWindowText(_T("Type one of the keys below to enter text.\nPress Exit to quit."));
m_wndOK.SetWindowText(_T("E&xit"));
m_bFirstTime = FALSE;
}
ShowWindow(SW_NORMAL); // activate myself
m_wndKeys.SetFocus(); // set focus to abbrev control
}
return 0;
}
////////////////
// Static helper fn to send a UNICODE string of text.
// This is the function that calls SendInput.
//
static void SendString(LPCTSTR str)
{
INPUT inp[2];
memset(inp,0,sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwFlags = KEYEVENTF_UNICODE; // to avoid shift, etc.
inp[1] = inp[0];
inp[1].ki.dwFlags |= KEYEVENTF_KEYUP;
for (LPCTSTR p=str; *p; p++) {
inp[0].ki.wScan = inp[1].ki.wScan = *p;
SendInput(2, inp, sizeof(INPUT));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -