0% found this document useful (0 votes)
41 views

Handling Keyboard Events

The document contains code for a CKeybrdEventsView class that is used to display and handle keyboard events in a view. The class overrides drawing and printing functions, gets the associated document, and contains a message handler for keyboard input that displays pressed keys on screen and saves them to a string until 'Z' is pressed.

Uploaded by

michael.ferraris
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Handling Keyboard Events

The document contains code for a CKeybrdEventsView class that is used to display and handle keyboard events in a view. The class overrides drawing and printing functions, gets the associated document, and contains a message handler for keyboard input that displays pressed keys on screen and saves them to a string until 'Z' is pressed.

Uploaded by

michael.ferraris
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM

// KeybrdEventsView.h : interface of the CKeybrdEventsView class

#if !
defined(AFX_KEYBRDEVENTSVIEW_H__CE1E8293_A3A6_46BE_B973_330
80B034966__INCLUDED_)
#define
AFX_KEYBRDEVENTSVIEW_H__CE1E8293_A3A6_46BE_B973_33080B034
966__INCLUDED_

#if _MSC_VER > 1000


#pragma once
#endif // _MSC_VER > 1000

class CKeybrdEventsView : public CView


{
protected: // create from serialization only
CKeybrdEventsView();
DECLARE_DYNCREATE(CKeybrdEventsView)

// Attributes
public:
CKeybrdEventsDoc* GetDocument();
int i,x;
char str[20],nChar;

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CKeybrdEventsView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CKeybrdEventsView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions


protected:
//{{AFX_MSG(CKeybrdEventsView)
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG // debug version in KeybrdEventsView.cpp


inline CKeybrdEventsDoc* CKeybrdEventsView::GetDocument()
{ return (CKeybrdEventsDoc*)m_pDocument; }
#endif

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the
previous line.
#endif // !
defined(AFX_KEYBRDEVENTSVIEW_H__CE1E8293_A3A6_46BE_B973_330
80B034966__INCLUDED_)

// KeybrdEventsView.cpp : implementation of the CKeybrdEventsView class

#include "stdafx.h"
#include "KeybrdEvents.h"

#include "KeybrdEventsDoc.h"
#include "KeybrdEventsView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CKeybrdEventsView

IMPLEMENT_DYNCREATE(CKeybrdEventsView, CView)

BEGIN_MESSAGE_MAP(CKeybrdEventsView, CView)
//{{AFX_MSG_MAP(CKeybrdEventsView)
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,
CView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CKeybrdEventsView construction/destruction
CKeybrdEventsView::CKeybrdEventsView()
{
// TODO: add construction code here
x=100;
i=0;
}

CKeybrdEventsView::~CKeybrdEventsView()
{
}

BOOL CKeybrdEventsView::PreCreateWindow(CREATESTRUCT& cs)


{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

// CKeybrdEventsView drawing

void CKeybrdEventsView::OnDraw(CDC* pDC)


{
CKeybrdEventsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
// CKeybrdEventsView printing

BOOL CKeybrdEventsView::OnPreparePrinting(CPrintInfo* pInfo)


{
// default preparation
return DoPreparePrinting(pInfo);
}
void CKeybrdEventsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo*
/*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CKeybrdEventsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)


{
// TODO: add cleanup after printing
}

// CKeybrdEventsView diagnostics

#ifdef _DEBUG
void CKeybrdEventsView::AssertValid() const
{
CView::AssertValid();
}

void CKeybrdEventsView::Dump(CDumpContext& dc) const


{
CView::Dump(dc);
}

CKeybrdEventsDoc* CKeybrdEventsView::GetDocument() // non-debug version


is inline
{
ASSERT(m_pDocument-
>IsKindOf(RUNTIME_CLASS(CKeybrdEventsDoc)));
return (CKeybrdEventsDoc*)m_pDocument;
}
#endif //_DEBUG

// CKeybrdEventsView message handlers


void CKeybrdEventsView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT
nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar=='Z')
{
str[i]='\0';
MessageBox(str);
}
CDC* pDC=GetDC();
pDC->TextOut(x,250,nChar);
x=x+10;
str[i]=nChar;
i=i+1;

CView::OnKeyDown(nChar, nRepCnt, nFlags);


}

OUTPUT

You might also like