How do you retrieve stylus pressure information on windows?
Asked Answered
I

4

7

Is anyone aware of a sane way to get tablet/stylus pressure information on Windows?

It's possible to distinguish stylus from mouse with ::GetMessageExtraInfo, but you can't get any more information beyond that. I also found the WinTab API in a out of the way corner of the Wacom site, but that's not part of windows as far as i can tell, and has a completely distinct event/messaging system from the message queue.

Given all I want is the most basic pressure information surely there is a standard Win32/COM API, is anyone aware of what it might be?

Insured answered 3/1, 2009 at 6:31 Comment(0)
B
6

The current way to do this is to handle WM_POINTERnnn msgs. Note this is for Win 8 and later.

Note you will get these msgs for touch AND pen, so you'll need to know the pointerType in order to test for pen. The WPARAM received by a WNDPROC for WM_POINTERnnnn msgs such a WM_POINTERUPDATE and other msgs contains the pointer id which you will need in order to request more info. Empirically I found that WM_POINTERUPDATE results in info that contains pressure data whereas if the pointer flags indicate down/up there is no pressure info.

const WORD wid = GET_POINTERID_WPARAM(wParam);
POINTER_INFO piTemp = {NULL};
GetPointerInfo(wid, &piTemp);
if (piTemp.pointerType == PT_PEN
{
    UINT32 entries = 0;
    UINT32 pointers = 0;

    GetPointerFramePenInfoHistory(wid, &entries, &pointers, NULL); // how many
    // TODO, allocate space needed for the info, process the data in a loop to retrieve it, test pointerInfo.pointerFlags for down/up/update.

}

Once you know you are dealing with pen, you can get the pressure info from the POINTER_PEN_INFO struct.

This is similar to handling touch although for touch you'd want gesture recognition and inertia. There is a Microsoft sample illustrating using these functions.

It's part of a Build talk: https://channel9.msdn.com/Events/Build/2013/4-022

Benedictbenedicta answered 8/5, 2017 at 15:49 Comment(0)
A
2

You need to use the Tablet PC Pen/Ink API. The COM version of the API lives in InkObj.dll. Here is a starting point for documentation: http://msdn.microsoft.com/en-us/library/ms700664.aspx

If I remember correctly, InkObj.dll is available on Windows XP SP2 and all later Windows client OSes, regardless of whether the machine is a Tablet PC.

Allamerican answered 9/4, 2009 at 2:6 Comment(0)
F
2

UPDATE:
It's been a number of years since I initially provided this answer, but wintab has become the de facto standard, and Ntrig more or less folded, eventually building a wrapper to allow for the wintab API to be accessed via this digitizer.
(http://www.tabletpcbuzz.com/showthread.php?37547-N-trig-Posts-WinTAB-Support-Driver)

This is a pretty late response, but recently my wife and I purchased a Dell XT tablet PC, which as it turns out actually uses NTrig, a suite of interfaces that utilize Ink, the accepted new windows API that shipped with Windows XP Tablet edition, then SP 2 and all versions thereafter.

A lot of Wacom tablets and others use the Wintab API, which is not currently open nor really permitted to use. From what I hear the folks who maintain it are pretty sue-happy.

So it depends on what type of tablet you're using, and the drivers you have installed for it. In my biased opinion, you should work with Ink, as it provides (or at least through NTrig and Windows 7 WILL provide) multi-touch capability and will likely be the new standard for tablet interfaces. But as of now, NTrig devices do not translate their pressure and angle information to common Wintab-based applications, such as Photoshop or Corel Painter. The applications tend to require at least some support for Microsoft's Tablet API in order to function properly.

Francium answered 20/7, 2009 at 16:11 Comment(2)
FYI, since I often hit this answer in my searches about wintab: The wintab API is still commonly used for pen tablet interactions and appears to be the de facto standard, at least until more applications and tablet manufacturers retrofit Microsoft Ink support. The folks who maintain the specification currently are Wacom, and they were on the receiving end of the lawsuit that I assume you're referring to. Their website welcomes other manufacturers to talk to them about implementing the wintab spec with their own hardware.Felipa
Wow, four years later! I've found what you say to be true, although the lawsuit I was referring to was Wacom suing someone for attempting to port wintab into their native interface. That being said, NTrig has written a wrapper to allow for the wintab API, and now my old Dell XT2 works with photoshop et al. I'll update this answer.Francium
S
1

If using UWP Windows Runtime then it's quite straightforward. The PointerEventArgs event seems to have all necessary data.

Modified Core App (C++/WinRT) template project snippet from Visual Studio 2019:

void OnPointerMoved(IInspectable const &, PointerEventArgs const &args)
{
    if (m_selected)
    {
        float2 const point = args.CurrentPoint().Position();

        m_selected.Offset(
            {
                point.x + m_offset.x,
                point.y + m_offset.y,
                0.0f
            });

        // (new!) Change sprite color based on pen pressure and tilt
        auto sprite = m_selected.as<SpriteVisual>();
        
        auto const props = args.CurrentPoint().Properties();
        auto const pressure = props.Pressure();
        auto const orientation = props.Orientation() / 360.0f;
        auto const tiltx = (props.XTilt() + 90) / 180.0f;
        auto const tilty = (props.YTilt() + 90) / 180.0f;

        Compositor compositor = m_visuals.Compositor();
        sprite.Brush(compositor.CreateColorBrush({
            (uint8_t)(pressure * 0xFF),
            (uint8_t)(tiltx * 0xFF),
            (uint8_t)(tilty * 0xFF),
            (uint8_t)(orientation * 0xFF)
            }));
    }
}

Similar code will likely work in C#, JavaScript, etc.

Staford answered 24/9, 2020 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.